C# 删除具有长路径的文件夹

标签 c# directory

我正在尝试删除文件夹,但由于文件夹包含长路径,删除失败。我想我需要使用其他东西而不是 dir.Delete(true),以前有人穿过这座桥吗?

非常感谢

 try
{
 var dir = new DirectoryInfo(@FolderPath);
 dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
 dir.Delete(true);
}
catch (IOException ex)
{
 MessageBox.Show(ex.Message);
}

这是有问题的路径: \server\share\dave\Private\Careers\Careers Ed\Fun Careers Education\Chris's not used 2006 to07\old 4.Careers Area Activity Week 1 30.10.06 or 6.11.06 or 13.11.06 工作级别和职业资源介绍\Occupational Areas & Job levels 导师帮助表[1].doc

最佳答案

In the Windows API, 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<NUL>" where "<NUL>" 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.) [MSDN]

几个函数的 Unicode 版本允许最大路径长度约为 32,000 个字符,由长度最多为 255 个字符的组件组成。要指定那种路径,请使用 "\\?\"字首。 32,000 个字符的最大路径是近似值,因为 "\\?\"前缀可以扩展为更长的字符串,扩展适用于总长度。

例如,"\\?\D:\<path>" .要指定这样的 UNC 路径,请使用 "\\?\UNC\"字首。例如,"\\?\UNC\<server>\<share>" .这些前缀不用作路径本身的一部分。它们表明路径应以最少的修改传递给系统,这意味着您不能使用正斜杠来表示路径分隔符,也不能使用句点来表示当前目录。此外,您不能使用 "\\?\"以相对路径为前缀。相对路径限制为 MAX_PATH 个字符。

shell 和文件系统可能有不同的要求。可以使用 shell UI 无法处理的 API 创建路径。

C#语法:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool DeleteFile(string path);

有关该类(class)的更多信息,请参阅 System Namespace - MSDN

摘自:

Filesystem Paths: How Long is Too Long? - Coding Horror

DeleteFile function (Windows) - MSDN

关于C# 删除具有长路径的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2223007/

相关文章:

c# - 表格中的 Razor 异常错误行号

windows - 如果超过 10 个,将删除最新文件夹的批处理文件

flash - java中如何确定包名,为什么 "com"作为root?

c# - 在属性中使用类型参数以将 ProducesResponseType 与泛型类型参数一起使用的解决方法?

c# - 单声道任务并行库实现?

c# - 使用 vNext 在 Entity Framework 7 中未添加导航属性

c# - 从 ServiceStack WSDL 中移除 swagger

Linux CentOS路径不存在

bash - 如何检查给定目录是否可访问?

C++ 获取目录前缀