public DirectoryDetails(string path, bool autoScanning = false)
        {
            Path        = path;
            ProgressVal = 0;

            if (autoScanning)
            {
                var scanningMsg = new ScanningMsg();
                var dd          = AutoScanning(path, scanningMsg);

                Size           = dd.Size;
                Subdirectories = dd.Subdirectories;
                Subfiles       = dd.Subfiles;
            }
            else
            {
                Path           = path;
                Size           = 0;
                Subdirectories = new List <DirectoryDetails>();
                Subfiles       = new List <string>();
            }
        }
        private static DirectoryDetails AutoScanning(string path, ScanningMsg msg, double progressVal)
        {
            var dd       = new DirectoryDetails(path);
            var taskPool = new List <Task>();

            msg.CurrentScanningPath = path;

            dd.ProgressVal = progressVal;

            try
            {
                var subdirectories = Directory.GetDirectories(path);
                var subfiles       = Directory.GetFiles(path);

                double eachProgressVal = subdirectories.Length != 0 ? dd.ProgressVal / subdirectories.Length : 0;
                dd.ProgressVal = subdirectories.Length != 0 ? 0 : dd.ProgressVal;

                foreach (var dir in subdirectories)
                {
                    if (threadsCount < processorCount * 5)
                    {
                        taskPool.Add(Task.Run(() =>
                        {
                            var ddChild = AutoScanning(dir, msg, eachProgressVal);

                            dd.Subdirectories.Add(ddChild);
                            dd.Size += ddChild.Size;
                        }));

                        threadsCount++;
                    }
                    else
                    {
                        var ddChild = AutoScanning(dir, msg, eachProgressVal);

                        dd.Subdirectories.Add(ddChild);
                        dd.Size += ddChild.Size;
                    }
                }

                foreach (var file in subfiles)
                {
                    dd.Subfiles.Add(file);
                    dd.Size += new FileInfo(file).Length;
                }

                // 线程必须全部完成才能结束
                Task.WaitAll(taskPool.ToArray());

                // 汇报进度
                msg.progressVal += dd.ProgressVal / 10000;
            }
            catch (UnauthorizedAccessException)
            {
                msg.progressVal += dd.ProgressVal / 10000;
                msg.WarningMsg  += ("Unauthoried: " + path + "\r\n");
            }
            catch (PathTooLongException)
            {
                msg.progressVal += dd.ProgressVal / 10000;
                msg.WarningMsg  += ("PathTooLong: " + path + "\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(dd);
        }
 public static DirectoryDetails AutoScanning(string path, ScanningMsg msg)
 {
     return(AutoScanning(path, msg, MAX_PROGRESS_VAL));
 }