zoukankan      html  css  js  c++  java
  • 错误及异常处理-[PathTooLongException]指定的路径或文件名太长

    错误信息

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

    环境

    NET 4.5

    原因分析

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

    解决方案

    方案1

    使用第三方开源库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)类型时,还是会因为文件名长度问题,报长度或者其他异常。

    方案2

    使用第三方开源库AlphaFS,它为.NET平台提供比标准System.IO类更完整的Win32文件系统功能支持的功能。

    功能

    • 支持扩展长度路径(最长32000个字符)
    • 创建连接点/硬链接
    • 访问隐藏的卷
    • 卷的枚举
    • 事务性文件操作
    • 支持NTFS备用数据流(文件/文件夹)
    • 访问网络资源(SMB / DFS)
    • 创建和访问名称中包含前导/尾随空格的文件夹/文件
    • 支持自定义筛选和错误报告/恢复的文件夹/文件枚举器(访问被拒绝的例外)

    优点

    • Alphaleonis.Win32.Filesystem的类名以及接口很规范,能够无缝替换System.IO
    • 功能全面强大

    缺点

    库比较大(359KB)

    方案3

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

    方案4

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

    参考

  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/zerodai/p/10224946.html
Copyright © 2011-2022 走看看