c# - 如何改进这个 SUPERFAST 目录大小查找器?

标签 c# parallel-processing directory size

我有几个庞大的目录(由于遗留原因我无法重组)。

一个典型的目录可能包含 150K 个子目录,每个子目录都有嵌套目录,可能还有 4K 个文件。

我无法使用 du 从 Windows 资源管理器或通过 cygwin 获取目录大小。这些都只是持续处理几个小时。

我已经编写了自己的代码来解决这个问题 - 对于较小的文件夹来说我的速度非常快 - 但对于这些大型文件夹来说仍然很慢。

有人可以改进吗?

(如果您有完全不同的解决方案,我也很高兴听到它。)

var size = GetDirectorySize3b(@"C:\MyMassiveFolder");

        public long GetDirectorySize3b(string parentDirectory)
        {
            Int64 ttl = 0;
            Stopwatch sw = new Stopwatch();
            var dirs = Directory.GetDirectories(parentDirectory);
            var llDirs = SplitIntoLists(dirs.ToList<string>(), 10);
            ttl = ParallelDirSizeLLS(llDirs);
            return ttl;

        }

        public List<List<string>> SplitIntoLists(List<string> l, int numLists)
        {
            List<List<string>> lls = new List<List<string>>();

            int listLength = l.Count/numLists + 1;
            for (int i = 0; i < l.Count; i += listLength)
            {
                var partL = l.Skip(i).Take(listLength).ToList<string>();
                lls.Add(partL);
            }

            return lls;
        }

        public long ParallelDirSizeLLS(List<List<string>> lls)
        {

            _size = 0;

            Parallel.ForEach(lls,
                //new ParallelOptions { MaxDegreeOfParallelism = 30 },
                ParallelDirSizeL);

            return _size;
        }

        private void ParallelDirSizeL(List<string> l)
        {
            foreach (var dir in l)
            {

                var ds = GetDirectorySize3(dir);
                Interlocked.Add(ref _size, ds);
            }
        }

        public long GetDirectorySize3(string parentDirectory)
        {
            Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
            Scripting.Folder folder = fso.GetFolder(parentDirectory);
            Int64 dirSize = (Int64)folder.Size;

            Marshal.ReleaseComObject(fso);

            return dirSize;
        }

最佳答案

我不确定解决方案,但也许您可以尝试使用 Microsoft Indexing Service?它存储有关所有索引文件的信息,包括大小。

我找到了一些信息: http://www.thejoyofcode.com/Using_Windows_Search_in_your_applications.aspx

关于c# - 如何改进这个 SUPERFAST 目录大小查找器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794429/

相关文章:

android - 如何使用 kotlin 协程并行运行两个作业但等待另一个作业完成

linux - 如何检查和删除python中两个不同目录中不相同的文件?

javascript - 在目录中打开/保存 Excel 文件

.htaccess - 如何隐藏我的网站(共享主机)的目录列表?

c# - 如何在 blazor 服务器端本地化验证消息 (DataAnnotationsValidator)

c# - NullReferenceException 与 ArgumentNullException

c# - IntPtr 究竟是什么?

c++ - OpenMP:一次为线程分配一个迭代

c# - 视频实时模糊

algorithm - 数据结构 Parallel Add Serial Remove Needed