避免最大字符限制 C#
要超出 C# 中的最大路径限制,您显然需要在驱动器路径的开头用 @"\\?\"
连接驱动器路径。如果我这样做,那么我会得到前面带有以下内容的驱动器路径
\\\\?\\\\\\server\\share\\...
现在,如果我查找该文件/folder 由于路径中存在非法字符(我假设为 ?
),它将失败,所以我如何采用 Microsoft 网站上概述的方法(http://msdn.microsoft.com/en-us/library /aa365247(VS.85).aspx) 正确吗?
foreach (string filePath in Directory.GetFiles(folder))
{
String s = @"\\?\" + filePath;
if (filePath.Length > 255)
{
Console.WriteLine(filePath);
}
if (File.Exists(filePath))
{
FileInfo finfo = new FileInfo(s);
folderSize += finfo.Length;
}
}
foreach (string dir in Directory.GetDirectories(folder))
folderSize += GetDirectorySize(dir);
To exceed the max path limit in c# you apparently need to concatenate your drive path with @"\\?\"
at the beginning of it. If I do this then I get a drive path with the following at the front
\\\\?\\\\\\server\\share\\...
Now if I look for the file/folder it will fail because of illegal charachters in the path (I assume the ?
) so how can I adopt the approach outlined on Microsoft's website (http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx) correctly?
foreach (string filePath in Directory.GetFiles(folder))
{
String s = @"\\?\" + filePath;
if (filePath.Length > 255)
{
Console.WriteLine(filePath);
}
if (File.Exists(filePath))
{
FileInfo finfo = new FileInfo(s);
folderSize += finfo.Length;
}
}
foreach (string dir in Directory.GetDirectories(folder))
folderSize += GetDirectorySize(dir);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准 System.IO 不支持超过 260 个字符的路径。
但似乎有一个具有扩展长度路径支持的库:
http://alphafs.codeplex.com/
我个人到目前为止还没有尝试过。
Standard System.IO doesn't support path longer than 260 characters.
But it seems there's a library with extended-length path support:
http://alphafs.codeplex.com/
I personally haven't tried it so far.
我认为您不应该在路径之前添加所有这些斜杠,我认为您不理解 MSDN 中所写的所有内容:
正如您可以在那里读到的:
这是问题的关键,如果您需要创建或访问长度超过 260 个字符的路径,则应使用特定的 Windows API。
I don't think you should add all those slashes before the path, I think you did not understand everything what was written in MSDN here:
as you can read there,:
this is the key for your issue, if you need to create or access to a path longer than ~260 chars you should use specific Windows APIs.