在代码中获取硬盘上的最大目录大小

发布于 2024-10-25 22:33:38 字数 348 浏览 1 评论 0原文

我的机器有一些问题。我的硬盘大约有 500 GB,我确信我的文件不超过 150 GB。但显示可用空间只有100GB。我会知道我的计算机中最大的目录是什么,因为当我尝试使用 "*.*" size:gigabytes 属性进行搜索时,这并不重要。我需要一个在目录中搜索然后在其子目录中搜索的算法的想法?例如

long count = Directory.GetDirectories("C:\\");
for (int i = 0; i < count.Length; i++)
{
   // I need to look in each directory and repeat process
}

I have some troubles in my machine. My harddisk is about 500 GBs, and I am sure my files are no more than 150 GB. But it shows that the free space is only 100 GB. I would know what is the biggest directory in my computer, because when I tried searching with "*.*" size:gigabytes property it doesn't matter. I need an idea of an algorithm that searches in directory then in its subdirectories? e.g.

long count = Directory.GetDirectories("C:\\");
for (int i = 0; i < count.Length; i++)
{
   // I need to look in each directory and repeat process
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多情癖 2024-11-01 22:33:39

你可以安装 cygwin 并执行以下操作:

$ du /cygdrive/c

You could just install cygwin and do:

$ du /cygdrive/c
赠佳期 2024-11-01 22:33:38

您需要了解递归或使用堆栈

我可以为您编写代码,但如果您打算更频繁地编写程序,那么这些是需要理解的基本概念。付出一些努力来很好地理解它们。然后,您将能够完成此任务以及许多其他任务。

You'll either need to learn about recursion or use a Stack.

I could write out the code for you, but if you plan to write programs more often, these are essential concepts to understand. Put in a bit of effort to understand them well. Then, you'll be able to do this task as well as many others.

不必你懂 2024-11-01 22:33:38

您是否需要构建一个程序来执行此操作,或者您只是尝试使用代码来为您自己的机器解决这个问题?

如果是后者,WinDirStat 将为您节省大量时间。

如果是前者,我建议您研究递归函数

Do you need to build a program to do this, or are you just trying to use code to figure this out for your own machine?

If it is the later, WinDirStat will save you a lot of time.

If it is the former, I suggest you look into recursive functions.

稳稳的幸福 2024-11-01 22:33:38

我还建议您尝试使用WinDirStat,它是开源软件。
我在这里分享使用此实用程序的经验, http ://lazyswamp.blogspot.kr/2014/11/windirstat-for-finding-files-big-but.html

I also recommend you to try to use WinDirStat, which is open-source software.
I share my experience on using this utility here, http://lazyswamp.blogspot.kr/2014/11/windirstat-for-finding-files-big-but.html.

追星践月 2024-11-01 22:33:38

这里是一个起点,它将递归调用C:驱动器并计算每个文件夹的大小。您可能想更改它以满足您的需要,例如不保留字典,而只保留最大的字典。

Func<DirectoryInfo, long> calc = null;
var sizes = new Dictionary<string, long>();
calc = di =>
            {
                var childSum = di.GetDirectories().Sum(d => calc(d));
                var size = di.GetFiles().Sum(f => f.Length);
                sizes.Add(di.FullName, childSum + size);
                return size;
            };
calc(new DirectoryInfo("C:\\"));

编辑
汉斯指出,您可能希望以提升的权限运行该程序,以窥探您可能无权访问的目录,例如系统卷信息。

Here is a starting point, it will recursively call the C: drive and calculate the size of each folder. You may want to change it to suit your needs, like rather than keep a dictionary, just keep the biggest one.

Func<DirectoryInfo, long> calc = null;
var sizes = new Dictionary<string, long>();
calc = di =>
            {
                var childSum = di.GetDirectories().Sum(d => calc(d));
                var size = di.GetFiles().Sum(f => f.Length);
                sizes.Add(di.FullName, childSum + size);
                return size;
            };
calc(new DirectoryInfo("C:\\"));

EDIT:
To Hans point, you may want to run the program with elevated permissions to snoop in directories you might not have access to, like System Volume Information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文