现在的位置: 首页 > 综合 > 正文

.Net 中 处理长文件名或者路径名

2013年10月05日 ⁄ 综合 ⁄ 共 1034字 ⁄ 字号 评论关闭

最近 在工作中遇到一些代码 生成的临时文件 文件或者目录名太长(超过260个)

直接调用.net framework IO 操作willPathTooLong exception as below:

File.Delete(LongPathFile);

or Driectory.Delete(longPathdurctory,true);

Exception:  qalified file name must be less than 260 characters, and the directory name must be less than
248 characters

when we encount this issue in .net ,we can call win32 api directly with prefix
\\?\+localPath or
\\?\UNC\UNCpah
to delete it .

  [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

         publicstatic extern int DeleteFile(string fileName);

         [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

         public tatic extern int RemoveDirectory(string pathName);

  StringBuilder  file = new StringBuilder(@"\\?\UNC\192.168.1.110\d$\longfolder\");

                string path =@"\\?、¡éUNC\192.168.1.110\d$\longfolder\";

                for(int i = 0; i < 250; i++)

                {

                    file.Append("3");

                }

                file.Append(".txt");

          File.Delete(file.ToString());

                DeleteFile(file.ToString());

                RemoveDirectory(path).

it is ok now. 微笑

 

抱歉!评论已关闭.