System.IO.PathTooLongException:“指定的路径或文件名太长,或者两者都太长。完全限定文件名必须少于 260 个字符,并且目录名必须少于 248 个字符。”

NET 4.5

在C#API中读取文件或文件夹时,完全限定文件名必须少于 260 个字符,并且目录名必须少于 248 个字符。( System.IO 源码中做的限制)

使用第三方开源库 ZetaLongPaths ,NuGet中使用1.0.0.24的版本,更高版本需要 NET 4.5.2 框架。

  • ZlpFileInfo- 类似于 System.IO.FileInfo 的类,它包装函数以处理文件路径。
  • ZlpDirectoryInfo- 类似于 System.IO.DirectoryInfo 的类,它包装函数以处理文件夹路径。
  • ZlpIOHelper-一组静态函数,以提供类似的功能,作为ZlpFileInfo与ZlpDirectoryInfo类,但在静态上下文。
  • ZlpPathHelper- 一组类似于 System.IO.Path 的静态函数,用于路径。
  • 代码示例及比较

    ZetaLongPaths.ZlpPathHelper.Combine(Path, tpName);
    //System.IO.Path.Combine(Path, tpName);
    ZetaLongPaths.ZlpPathHelper.Combine(null, null, null, names.ToArray());
    //System.IO.Path.Combine(names.ToArray());
    ZetaLongPaths.ZlpIOHelper.GetFiles(dirpath, "*");
     //System.IO.Directory.GetFiles(dirpath, "*.*");
    ZetaLongPaths.ZlpPathHelper.GetFileNameWithoutExtension(mDir);
    //System.IO.Path.GetFileNameWithoutExtension(m_Dir);
    ZetaLongPaths.ZlpIOHelper.DeleteFile(filePath);
    //System.IO.File.Delete(filePath);
    var directory = new ZlpDirectoryInfo(dirPath);
    //var directory = new DirectoryInfo(dirPath);
    ZetaLongPaths.ZlpPathHelper.GetDirectoryPathNameFromFilePath(path);
    //System.IO.Path.GetDirectoryName(path);
    ZetaLongPaths.ZlpIOHelper.DirectoryExists(folderPath);
    //System.IO.Directory.Exists(folderPath);
    ZetaLongPaths.ZlpIOHelper.CreateDirectory(folderPath);
    //Directory.CreateDirectory(folderPath);
    
  • 能满足大部分文件和文件夹的操作
  • 轻量(65KB)
  • System.IO的接口规范有不小的区别,使用时需要逐个确认并替换
  • 缺少对文件流(System.IO.File)的相关操作的支持,例如使用File.Open(string path, FileMode mode)或者new FileStream(string path, FileMode mode)类型时,还是会因为文件名长度问题,报长度或者其他异常。
  • 使用第三方开源库AlphaFS,它为.NET平台提供比标准System.IO类更完整的Win32文件系统功能支持的功能。

  • 支持扩展长度路径(最长32000个字符)
  • 创建连接点/硬链接
  • 访问隐藏的卷
  • 事务性文件操作
  • 支持NTFS备用数据流(文件/文件夹)
  • 访问网络资源(SMB / DFS)
  • 创建和访问名称中包含前导/尾随空格的文件夹/文件
  • 支持自定义筛选和错误报告/恢复的文件夹/文件枚举器(访问被拒绝的例外)
  • Alphaleonis.Win32.Filesystem的类名以及接口很规范,能够无缝替换System.IO
  • 功能全面强大
  • 库比较大(359KB)

    使用“\\?\”作为文件名前缀并调用Windows API的Unicode版本

    升级框架NET 4.6.2及其以上版本以解决上述bug

  • Announcing .NET Framework 4.6.2
  • More on new .NET path handling
  • Long Paths in .NET
  •