[英]Error: File Path is Too Long
我试图在C#中使用各种文件函数,如File.GetLastWriteTime
,复制命令放在路径上的文件大于Windows 7上的最大允许路径,即260.它给我一个长路径名错误。 在MSDN支持上,他们要求在路径前使用\\\\?\\
。 我做了同样的但仍然得到了同样的错误,它似乎没有做任何改变。 以下是我的代码。 如果我使用它正确或我需要添加任何东西,请告诉我:
我使用的所有lib作为代码还有其他东西:
以下是各自的代码:
filesToBeCopied = Directory.GetFiles(path,"*",SearchOption.AllDirectories);
for (int j = 0; j < filesToBeCopied.Length; j++)
{
try
{
String filepath = @"\\?\" + filesToBeCopied[j];
File.GetLastWriteTime(filepath);
}
catch (Exception ex)
{
MessageBox.Show("Error Inside the single file iteration for the path:" +
filesToBeCopied[j] + " . The exception is :" + ex.Message);
}
}
其中path是Windows机器上以驱动器号开头的文件夹的路径。 例如: d:\\abc\\bcd\\cd\\cdc\\dc\\..........
这是至少您的请求的复制部分的解决方案(谢谢pinvoke.net ):
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
然后实际复制你的文件:
// Don't forget the '\\?\' for long paths
string reallyLongPath = @"\\?\d:\abc\bcd\cd\cdc\dc\..........";
string destination = @"C:\some\other\path\filename.txt";
CopyFile(reallyLongPath , destination, false);
据我所知,如果文件的路径太长,则无法直接访问文件(直接使用File
的方法,通过构造函数创建FileInfo
,或使用Directory.GetFiles(string fileName)
。
我发现的唯一方法,可以让你访问这样的文件是在某处访问的目录路径它变得不久,然后编程走在树,直到你得到你的文件,因为看到这里 。
我从那里获取了我的代码并对其进行了一些修改以返回一个FileInfo
对象,该文件的路径为“太长”。 使用此代码,您可以在返回的FileInfo
对象(如LastWriteTime
)上访问必要的属性 。 它仍然有一些局限性,比如无法使用CopyTo()
或OpenText()
等函数。
// Only call GetFileWithLongPath() if the path is too long
// ... otherwise, new FileInfo() is sufficient
private static FileInfo GetFile(string path)
{
if (path.Length >= MAX_FILE_PATH)
{
return GetFileWithLongPath(path);
}
else return new FileInfo(path);
}
static int MAX_FILE_PATH = 260;
static int MAX_DIR_PATH = 248;
private static FileInfo GetFileWithLongPath(string path)
{
string[] subpaths = path.Split('\\');
StringBuilder sbNewPath = new StringBuilder(subpaths[0]);
// Build longest sub-path that is less than MAX_PATH characters
for (int i = 1; i < subpaths.Length; i++)
{
if (sbNewPath.Length + subpaths[i].Length >= MAX_DIR_PATH)
{
subpaths = subpaths.Skip(i).ToArray();
break;
}
sbNewPath.Append("\\" + subpaths[i]);
}
DirectoryInfo dir = new DirectoryInfo(sbNewPath.ToString());
bool foundMatch = dir.Exists;
if (foundMatch)
{
// Make sure that all of the subdirectories in our path exist.
// Skip the last entry in subpaths, since it is our filename.
// If we try to specify the path in dir.GetDirectories(),
// We get a max path length error.
int i = 0;
while (i < subpaths.Length - 1 && foundMatch)
{
foundMatch = false;
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
if (subDir.Name == subpaths[i])
{
// Move on to the next subDirectory
dir = subDir;
foundMatch = true;
break;
}
}
i++;
}
if (foundMatch)
{
// Now that we've gone through all of the subpaths, see if our file exists.
// Once again, If we try to specify the path in dir.GetFiles(),
// we get a max path length error.
foreach (FileInfo fi in dir.GetFiles())
{
if (fi.Name == subpaths[subpaths.Length - 1])
{
return fi;
}
}
}
}
// If we didn't find a match, return null;
return null;
}
既然你已经看到了,那就去冲洗你的眼睛并缩短你的路径。
尝试使用此代码
var path = Path.Combine(@"\\?\", filesToBeCopied[j]); //don't forget extension
路径字符串的“\\?\\”前缀告诉Windows API禁用所有字符串解析并将其后面的字符串直接发送到文件系统。
重要提示:并非所有文件I / O API都支持“\\?\\”,您应该查看每个API的参考主题
http://www.codinghorror.com/blog/2006/11/filesystem-paths-how-long-is-too-long.html
我最近为超过256个字符的最大路径限制的客户导入了一些源代码。
您粘贴的路径长度为285个字符。
正如您在评论中所述,MSDN的链接( http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maximum%5Fpath%5Flength )更详细地解释了这个长度:
在Windows API中(以下段落中讨论了一些例外),路径的最大长度为MAX_PATH,定义为260个字符 。 本地路径按以下顺序构成:驱动器号,冒号,反斜杠,由反斜杠分隔的名称组件以及终止空字符。 例如,驱动器D上的最大路径是“D:\\某个256个字符的路径字符串”,其中“”表示当前系统代码页的不可见的终止空字符。 (这里使用字符<>是为了清晰,不能成为有效路径字符串的一部分。)
关于\\\\?\\
功能:
许多但不是所有文件I / O API都支持“\\?\\”; 您应该查看每个API的参考主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.