即使我使用缩短的路径,(。Net)FileInfo()也无法说明它太长!
我在使用长路径时遇到问题,所以我对(Win32)GetShortPathNameW进行了调用,该方法返回了一个很好雕刻的短名称,例如:
“ C:\ temp \ Test \ targ1 \ FIRSTH〜1 \ SECOND〜1 \ SumStuff.txt.somethingTooLong.junk”。
即使长度只有72个字符,(。Net)FileInfo()也会引发异常:“指定的路径,文件名或两者都太长。完全限定的文件名必须少于260个字符,并且目录名称必须少于248个字符。”
是的,LFN太长(> 400个字符),但缩短时看起来不错。 .Net将其转换回长吗?
(在Windows 10下使用.Net 3.5(最低公分母)。
// code below is extremely abbreviated
[DllImport ( "kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int GetShortPathNameW ( string path, StringBuilder Buffer, int bufferSize);
…
int PathLength = GetShortPathNameW ( Path_, buffer, buffer.Capacity);
…
FileInfo FileMeta = new FileInfo ( buffer.ToString());
// is fine if the LFN is short (<260)
// but exception using a shortened path name
好的,我添加了我编译并运行的该示例,但有相同的错误。我忍不住觉得Unicode /编组的辩论分散了我的真实想法:
// simplified example (should have started with this)
static void Main ( string[] args)
{
FileInfo FileMeta = new FileInfo ( @"C:\temp\Test\targ1\FIRSTH~1\SECOND~1\SumStuff.txt.somethingTooLong.junk");
Console.WriteLine ( "Size: " + FileMeta.Length.ToString());
}
仍然获得相同的回报:
Unhandled Exception: System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
at System.IO.Path.SafeSetStackPointerValue(Char* buffer, Int32 index, Char value)
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.FileInfo..ctor(String fileName)
at Test.Program.Main(String[] args) in E:\Projects\Test\Test\Program.cs:line 12