如何在int变量中求和变化值?什么是硬盘上的实际目录数?

时间:2016-03-14 16:18:29

标签: c# .net winforms

Backgroundworker dowork事件

string CurrentFileWithPath;
        private void _FileProcessingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                DirectoryInfo[] MySubDirectories = (DirectoryInfo[])e.Argument;
                for (int i = 0; i < MySubDirectories.GetLength(0); i++)
                {
                    DirectoryInfo MySubDirectory = MySubDirectories[i];

                    List<FileInfo> l = new List<FileInfo>();
                    CountFiles(MySubDirectory, l);

                    int totalFiles = l.Count;
                    object[] CurrentStatus = new object[5];
                    CurrentStatus[3] = i.ToString();
                    CurrentStatus[4] = totalFiles.ToString();
                    _FileProcessingWorker.ReportProgress(0, CurrentStatus);

                    string CurrentDirectory = "Current Directory: " + MySubDirectory.Name;

                    foreach (FileInfo MyFile in l)
                    {
                        CurrentStatus = new object[5];
                        if (_FileProcessingWorker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        if (MyFile.Extension.ToLower() == ".cs" || MyFile.Extension.ToLower() == ".vb")
                        {
                            string CurrentFile = "Current File: " + MyFile.Name;
                            string CurrentFileWithPath = MyFile.FullName;

                            CurrentStatus[0] = CurrentDirectory;
                            CurrentStatus[1] = CurrentFile;
                            _FileProcessingWorker.ReportProgress(0, CurrentStatus);

                            List<string> Result = SearchInFile(CurrentFileWithPath, "static class FileShellExtension");

                            if (Result != null && Result.Count > 0)
                            {
                                CurrentStatus[2] = Result;
                                _FileProcessingWorker.ReportProgress(0, CurrentStatus);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                return;
            }

Progresschanged事件

private void _FileProcessingWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (typeof(object[]) == e.UserState.GetType())
            {
                object[] StatusMsg = (object[])e.UserState;
                if (5 == StatusMsg.GetLength(0))
                {
                    label2.Text = StatusMsg[4].ToString();
                    label4.Text = StatusMsg[3].ToString();

                    if (StatusMsg[0] != null && StatusMsg[1] != null)
                    {
                        lblCurrentDirectory.Text = StatusMsg[0].ToString();
                        lblStatus.Text = StatusMsg[1].ToString();
                    }
                    if (StatusMsg[2] != null)
                    {
                        if (StatusMsg[2].GetType() == typeof(List<string>))
                        {
                            List<string> l = (List<string>)StatusMsg[2];

                            for (int i = 0; i < l.Count; i++)
                            {
                                ListViewCostumControl.lvnf.Items.Add("Directory: " + lblCurrentDirectory.Text + "In File: " + lblStatus.Text + l[i]);
                                w.WriteLine("Directory: " + lblCurrentDirectory.Text + "In File: " + lblStatus.Text + l[i]);
                            }
                        }
                    }
                }
            }
        }

CountFiles方法

private void CountFiles(DirectoryInfo di, List<FileInfo> l)
        {
            try
            {
                l.AddRange(di.EnumerateFiles());
            }
            catch
            {
                string fff = "";
            }

            try
            {
                IEnumerable<DirectoryInfo> subDirs = di.EnumerateDirectories();
                if (subDirs.Count() > 0)
                {
                    foreach (DirectoryInfo dir in subDirs)
                        CountFiles(dir, l);
                }
            }
            catch 
            {
                string yyy = "";
            }
        }

SearchInFile方法

private List<string> SearchInFile(string fileToSearch, string textToSearch)
        {
            List<string> l = new List<string>();
            try
            {
                foreach (var line in File.ReadAllLines(fileToSearch))
                {
                    if (line.Contains(textToSearch))
                        l.Add(line);
                }
            }
            catch(Exception err)
            {
                string fff = err.ToString();
            }

            return l;
        }

第一个问题是获取目录数量时:

private void btnProcess_Click(object sender, EventArgs e)
        {
            btnProcess.Enabled = false;
            btnDirectory.Enabled = false;
            btnCancel.Visible = true;
            btnCancel.Enabled = true;
            btnCancel.Text = "Cancel";
            MyProgressBar.Visible = true;
            _FileProcessingWorker = new BackgroundWorker();
            _FileProcessingWorker.WorkerReportsProgress = true;
            _FileProcessingWorker.WorkerSupportsCancellation = true;
            _FileProcessingWorker.DoWork += new DoWorkEventHandler(_FileProcessingWorker_DoWork);
            _FileProcessingWorker.ProgressChanged += new ProgressChangedEventHandler(_FileProcessingWorker_ProgressChanged);
            _FileProcessingWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_FileProcessingWorker_RunWorkerCompleted);
            string BasePath = lblDirectoryName.Text;
            DirectoryInfo MyDirectory = new DirectoryInfo(BasePath);
            DirectoryInfo[] MySubDirectories = MyDirectory.GetDirectories();
            int SubDirectoryCount = MySubDirectories.GetLength(0);
            MyProgressBar.Minimum = 0;
            MyProgressBar.Step = 1;
            MyProgressBar.Maximum = SubDirectoryCount;
            MyProgressBar.Value = MyProgressBar.Minimum;
            _LastCounter = 0;
            _FileProcessingWorker.RunWorkerAsync(MySubDirectories);
        }

在点击按钮中,我获取子目录。 但是有两个问题。 在dowork事件中,我在这一行上设置了一个断点:

for (int i = 0; i < MySubDirectories.GetLength(0); i++)

我看到它包含409个目录。但是当我向dowork事件中的label4报告目录数量时

CurrentStatus[3] = i.ToString();

在progresschanged事件中

 label4.Text = StatusMsg[3].ToString();

我最终在label4 408目录中看到了。在D :(在这种情况下,我正在我的D:\目录上工作)当我选择Windows资源管理器中的所有目录时,右键单击和属性我只看到405个目录。那么目录的真实数量是多少?我应该在label4中显示多少目录?

第二个问题是当我报告dowork事件中的文件数时:

CurrentStatus[4] = totalFiles.ToString();

并且在progresschange事件中显示在label2上:

label2.Text = StatusMsg[4].ToString();

问题是,totalFiles值一直在变化,一旦它变为2,那么它是768然后是它的66然后是8987但是我想要的是以某种方式实现总结时间值totalFiles所以在label2中我将第一次看到例如:2然后770然后(770 + 66)所以我将在label2 816 ....中看到总是看到目前为止所有值的总和。

最后一个问题是在这个循环的progresschanged事件中:

for (int i = 0; i < l.Count; i++)
                                {                                    ListViewCostumControl.lvnf.Items.Add("Directory: " + lblCurrentDirectory.Text + "In File: " + lblStatus.Text + l[i]);
                                    w.WriteLine("Directory: " + lblCurrentDirectory.Text + "In File: " + lblStatus.Text + l[i]);
                                }

这可能是逻辑某种方式在这个时候在另一个背景工作者如backgroundworker2 dowork事件的程序中取出并制作这个循环?如果是这样怎么办?问题是当循环中有许多项要添加到listView时它会使程序挂起直到循环结束。

1 个答案:

答案 0 :(得分:1)

每次执行

CurrentStatus[4] = totalFiles.ToString();

您正在将状态值重置为当前处理目录中的文件计数。这就是为什么数字一直在弹跳。相反,我建议:

int thisDirFiles;
thisDirFiles = l.Count;
totalFiles += thisDirFiles;
CurrentStatus[4] = totalFiles.ToString();

现在,当您报告进度时,CurrentStatus [4]将具有运行总计。

对于您的第二个问题,在您报告进度时,我认为您正在尝试做太多。操作ListView可能成本很高,并且您可能花费更多时间更新ListView而不是处理目录,因此您的计算机似乎冻结了。