[英]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.