在代码中获取硬盘上的最大目录大小
我的机器有一些问题。我的硬盘大约有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以安装 cygwin 并执行以下操作:
You could just install cygwin and do:
您需要了解递归或使用堆栈。
我可以为您编写代码,但如果您打算更频繁地编写程序,那么这些是需要理解的基本概念。付出一些努力来很好地理解它们。然后,您将能够完成此任务以及许多其他任务。
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.
您是否需要构建一个程序来执行此操作,或者您只是尝试使用代码来为您自己的机器解决这个问题?
如果是后者,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.
我还建议您尝试使用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.
这里是一个起点,它将递归调用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.
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.