简体   繁体   English

C#/ C ++-如何获取路径太长或权限被拒绝的目录的大小?

[英]C#/C++ - how to get size of directory where path is too long, or permission denied?

There are loads of questions and answers about how to get the size of a directory - recursively summing the length of the files - easy. 关于如何获取目录的大小(递归求和文件的长度),有很多问题和答案。

However all the answers I've seen are incomplete. 但是,我所看到的所有答案都是不完整的。 Where you recursively iterate over a volume there are directories that throw exceptions - UnauthorizedAccessException for example. 在递归地遍历卷的地方,有抛出异常的目录-例如UnauthorizedAccessException I've also noticed in Win 7 there are paths that exceed 260 characters, which throws a PathTooLongException . 我还注意到在Win 7中有超过260个字符的路径,该路径抛出PathTooLongException

All of which means, in C# at least, you cannot scan a C:\\ drive and get a real figure back for the size of the volume because you have to skip any directory or file that throws an exception. 所有这些意味着,至少在C#中,您无法扫描C:\\驱动器并无法获得卷的大小的真实数字,因为您必须跳过任何引发异常的目录或文件。

I am looking for a way of getting a proper, true, accurate figure for bytes stored on disk for a given directory. 我正在寻找一种获取给定目录的磁盘上存储的字节的正确,正确,准确的数字的方法。 I don't care if I have to go low level and write some C++, or call the Win APIs. 我不在乎是否必须走低级并编写一些C ++或调用Win API。

How can this be achieved? 如何做到这一点? Thanks. 谢谢。

Is not going to be easy but it can be done and while I am not sure that you can overcome the permissions issues essentially what you have to do is go on a lower level and read the NTFS master file table directly as shown in this wonderful piece of code shared by this caring developer who got the idea from another such developer. 这不是一件容易的事,但是可以做到,尽管我不确定您是否可以克服权限问题,但是本质上您必须要做的就是在较低级别上直接读取NTFS主文件表,如本精彩文章所示。 这个有同情心的开发人员共享的代码这些开发人员从另一个这样的开发人员那里得到了创意。

Here is a reference that will help you. 这是您有帮助的参考 Google 'NTFS architecture' or design for more specs. Google的“ NTFS体系结构”或为更多规格而设计。 And please share some of your work & research:) It will be helpful to many 并请分享您的一些研究工作:)

Edit : Another project which may help you. 编辑另一个可以帮助您的项目 After testing it says that it supports only NTFS 3.0 or higher. 经过测试后,它说它仅支持NTFS 3.0或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM