后台工作人员进度栏发布c#Visual Studio 2010 [英] BackgroundWorker & Progressbar Issues c# Visual Studio 2010

查看:95
本文介绍了后台工作人员进度栏发布c#Visual Studio 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的后台工作人员和进度条成为现实,到目前为止,我已经使它工作了,但并不是我想要的那样.基本上,我正在对文件夹进行排序/重命名并将它们复制到其他位置,这可以正常工作,并且代码是不言自明的,生成的输出文件夹与预期的一样.但是对于我要搜索的每个文件夹,我都必须右键单击它以获取文件数,然后在代码中必须将progressBar1.Maximum设置为该值,以使其显示进度条的coreect进度.无论如何都要通过每个文件夹自动设置文件数量?一些文件夹包含数千个文件,而其他文件夹则包含数百万个文件.除此之外,我想添加一个标签,以便显示正在处理的文件的名称以及进度条更新.

Im trying to get my head around backgroundworker and the progressbar, so far i have got it to work but not exactly how i want it to work. Basically i am sorting/renaming folders and copying them to a different location, this works and the code is self explanatory, the output folders generated are as expected. However for each folder i intend to search through i have to right click it to get the number of files and then in the code i have to set the progressBar1.Maximum to that value in order for it to show the coreect progression of the progress bar. How is it possible to get this to set the number of files automatically since it goes through each folder anyway? Some folders have thousands of files and others have millions. beyond this i want to add a label so that it displays the name of the file it is processing along with the progressbar updates.

namespace Data_Sorter
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
    }

    private void btnSort_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int totalFiles = 0;
        foreach (var file in Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories))
        {
            backgroundWorker1.ReportProgress(totalFiles);

            string fullFilename = file.ToString();

            string[] pathParts = fullFilename.Split('\\');
            string date = pathParts[6];

            string fileName = pathParts[7];

            string[] partName = fileName.Split('_');

            string point = partName[3];

            Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");

            if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                {
                    string destPath = (point + "\\" + date + "\\");
                    File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
            else
                {
                    MessageBox.Show("destination folder not found " + date + point);
                }

            totalFiles++;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Maximum = 6777; // set this value at the maximum number of files you want to sort //
        progressBar1.Value = e.ProgressPercentage;
    }
}

推荐答案

您只需读取GetFiles长度即可找到文件编号.

You can find out the file number simply reading the GetFiles length.

您可以使用表达式(i * 100) / totalFiles传递相对百分比,这样就不必设置进度的最大值.

You can pass the relative percentage using the expression: (i * 100) / totalFiles, in this way it's not necessary to set the Maximum value for the progress.

您还可以将文件名报告给进度条,将其作为用户状态传递给progressChanged事件.

You can also report the filename to the progressbar passing it as the UserState in the progressChanged event.

请尝试以下代码:

   namespace Data_Sorter
    {
    public partial class Form1 : Form
    {


public Form1()
    {
        InitializeComponent();
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
    }

    private void btnSort_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int totalFiles = 0;
        string[] files = Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories);
        totalFiles = files.Length;
        int i = 0;
        foreach (var file in files)
        {

            backgroundWorker1.ReportProgress((i * 100) / totalFiles, file);
            i++
            string fullFilename = file.ToString();

            string[] pathParts = fullFilename.Split('\\');
            string date = pathParts[6];

            string fileName = pathParts[7];

            string[] partName = fileName.Split('_');

            string point = partName[3];

            Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");

            if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                {
                    string destPath = (point + "\\" + date + "\\");
                    File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
            else
                {
                    MessageBox.Show("destination folder not found " + date + point);
                }

            totalFiles++;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        progressBar1.Text = e.UserState.ToString();//or yourNewLabel.Text = e.UserState.ToString();
    }
}

这篇关于后台工作人员进度栏发布c#Visual Studio 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆