[英]C# SHA256 on files with very long file paths
I get a List<string>
of files via recursion through FileFinderEx
via win32 api (DllImport of kernel32). 我通过FileFinderEx
通过win32 api(kernel32的DllImport)通过递归获得List<string>
文件。 (There's several questions that go over how this is done). (有几个问题可以解决这个问题)。
I handle fully qualified paths greater than MAX_PATH
by using \\\\?\\UNC\\
and \\\\?\\
. 我使用\\\\?\\UNC\\
和\\\\?\\
来处理大于MAX_PATH
的完全限定路径。
Also, from win32 api I can grab the "short path" (8.3 name) of file via GetShortPathNameW
. 另外,从win32 api我可以通过GetShortPathNameW
文件的“短路径”(8.3名称)。
Now that I have the List<string>
of fully qualified paths to files, I want to iterate over each path and place their SHA256
into another list (same index). 现在我有了文件的完全限定路径的List<string>
,我想迭代每个路径并将它们的SHA256
放入另一个列表(相同的索引)。 So roughly, something like this: 粗略地说,这样的事情:
List<string> files = new List<string>();
//win api function populates files ...
List<string> hash = new List<string>();
for (int i = 0; i < files.Count; i++)
{
using (var stream = new BufferedStream(File.OpenRead(files[i]), 1200000))
{
SHA256Managed sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
hash.Add(BitConverter.ToString(checksum).Replace("-", String.Empty));
stream.Close();
}
}
The problem I face is even if I give it the "short path" (8.3 name) the File.OpenRead
always throws exception if the fully qualified UNC name is greater than MAX_PATH
(260 I believe). 我面临的问题是即使我给它“短路径”(8.3名称),如果完全限定的UNC名称大于MAX_PATH
(我相信260), File.OpenRead
总是抛出异常。
So... now what? 那么现在怎么办? Is there some win32 api function I can import to do the reading instead of File.OpenRead
? 是否有一些win32 api函数我可以导入来执行读取而不是File.OpenRead
? Or is there some other .NET method to open files with long file paths? 或者是否有其他.NET方法来打开具有长文件路径的文件? Any tips to get around this limitation? 有什么提示可以解决这个限制吗?
Take a look at the CreateFile
function in kernel32.dll. 看看kernel32.dll中的CreateFile
函数。 Despite it's name, you can use it to either read or create a file, and the Unicode version will allow you to use paths longer than MAX_PATH
. 尽管它的名称,您可以使用它来读取或创建文件,Unicode版本将允许您使用长于MAX_PATH
路径。
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
internal static extern SafeFileHandle CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
There is a good series of posts (part 1 here ) on the BCL Team's blog from a few years back covering the topic of long paths in Windows and .NET, which I think you'd find useful. 几年前BCL团队的博客上有一系列很好的帖子( 这里的第1部分),涵盖了Windows和.NET中的长路径主题,我认为你会发现它很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.