避免最大字符限制 C#

发布于 2024-12-05 16:01:12 字数 826 浏览 0 评论 0原文

要超出 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技术交流群

发布评论

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

评论(2

左岸枫 2024-12-12 16:01:12

标准 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.

冷情 2024-12-12 16:01:12

我认为您不应该在路径之前添加所有这些斜杠,我认为您不理解 MSDN 中所写的所有内容:

最大路径长度限制

在 Windows API 中(除了下面讨论的一些例外情况)
段落),路径的最大长度为 MAX_PATH,即
定义为 260 个字符。本地路径的结构如下
顺序:驱动器号、冒号、反斜杠、名称组成部分分隔
反斜杠和终止空字符。例如,
驱动器 D 上的最大路径为“D:\some 256-character path string”
其中“”代表不可见的终止空字符
当前系统代码页。 (字符 < > 在这里用于
视觉清晰,不能成为有效路径字符串的一部分。)

注意: Windows API 中的文件 I/O 函数将“/”转换为“\”作为一部分
将名称转换为 NT 样式名称,除非使用
“\?\”前缀,详见以下各节。

Windows API 有许多函数也有 Unicode 版本
允许最大总路径长度的扩展长度路径
32,767 个字符。这种类型的路径由组件组成
用反斜杠分隔,每个反斜杠最多可达返回的值
GetVolumeInformation 的 lpMaximumComponentLength 参数
函数(该值通常为 255 个字符)。要指定一个
扩展长度路径,使用“\?\”前缀。例如,“\?\D:\非常
路漫漫其修远兮”。

注意: 32,767 个字符的最大路径是近似值,因为
“\?\”前缀可能会被系统扩展为更长的字符串
运行时间,并且此扩展适用于总长度。

正如您可以在那里读到的:

Windows API 有许多函数也有 Unicode 版本
允许最大总路径长度的扩展长度路径
32,767 个字符。

这是问题的关键,如果您需要创建或访问长度超过 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:

Maximum Path Length Limitation

In the Windows API (with some exceptions discussed in the following
paragraphs), the maximum length for a path is MAX_PATH, which is
defined as 260 characters. A local path is structured in the following
order: drive letter, colon, backslash, name components separated by
backslashes, and a terminating null character. For example, the
maximum path on drive D is "D:\some 256-character path string"
where "" represents the invisible terminating null character for
the current system codepage. (The characters < > are used here for
visual clarity and cannot be part of a valid path string.)

Note: File I/O functions in the Windows API convert "/" to "\" as part
of converting the name to an NT-style name, except when using the
"\?\" prefix as detailed in the following sections.

The Windows API has many functions that also have Unicode versions to
permit an extended-length path for a maximum total path length of
32,767 characters. This type of path is composed of components
separated by backslashes, each up to the value returned in the
lpMaximumComponentLength parameter of the GetVolumeInformation
function (this value is commonly 255 characters). To specify an
extended-length path, use the "\?\" prefix. For example, "\?\D:\very
long path".

Note: The maximum path of 32,767 characters is approximate, because
the "\?\" prefix may be expanded to a longer string by the system at
run time, and this expansion applies to the total length.

as you can read there,:

The Windows API has many functions that also have Unicode versions to
permit an extended-length path for a maximum total path length of
32,767 characters.

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.

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