1

我试图让我的头脑围绕背景工作人员和进度条,到目前为止我已经让它工作了,但不完全是我想要的工作方式。基本上我正在对文件夹进行排序/重命名并将它们复制到不同的位置,这有效并且代码是不言自明的,生成的输出文件夹与预期的一样。但是,对于我打算搜索的每个文件夹,我必须右键单击它以获取文件数,然后在代码中我必须将 progressBar1.Maximum 设置为该值,以便它显示进度条的 coreect 进度. 怎么可能让它自动设置文件的数量,因为它无论如何都要经过每个文件夹?一些文件夹有数千个文件,而另一些文件夹有数百万个文件。

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;
    }
}
4

2 回答 2

2

您只需读取 GetFiles 长度即可找出文件编号。

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

您还可以将文件名报告给进度条,在 progressChanged 事件中将其作为 UserState 传递。

试试下面的代码:

   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();
    }
}
于 2012-12-10T13:54:00.160 回答
1

向上移动对 GetFiles 的调用,以便获得它返回的数组的长度:

string[] files = Directory.GetFiles(tbFilePath.Text, "*.txt",
                                    SearchOption.AllDirectories));

// Note - you won't be able to set this UI property from DoWork 
// because of cross-thread issues:
// progressbar1.Maximum = files.Length; 

int fileCount = files.Length;   

foreach (var file in files ...
于 2012-12-10T13:51:48.170 回答