最近 在工作中遇到一些代码 生成的临时文件 文件或者目录名太长(超过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.