bool a = File.Exists(archiveDetail["downloadPath"]);
即使位置中存在指定的文件,a始终设置为false,当我调试并复制文件路径并将其粘贴到资源管理器中时,该文件存在并打开,但是此方法返回false。 长度文件路径也是309个字符,这是造成这种情况的原因吗?
文件路径的长度应小于260个字符吗? 另外,我还更改了app.confing以考虑路径较长的文件:
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>
为什么File.Exists()返回false?
试图处理超过260个字符的文件路径。 P.S-文件名的长度为134个字符。
我也尝试过这个[link]仍然没有用。
这是调试时生成的路径,当我将其粘贴到资源管理器中时,文件打开:
C:\Users\a.bhis\Documents\GitHub\Solutio-2000\Solutio\Solutio\Content\SDKs\web\Z10_\Solutions_ZC_265\Zrbeqtqdqkuqenle\SolutionExploer\ABC Privateino\Bhar\CasisInfo\ddtp___p123palsis01.extern_botgurndne_comfi_handbuerer_silly_zzp-e.nsf_5_ZZ73860ZZEZZZ8CZ6701244Z4646_$file_Lassiv+BN2+11-1999.pdf
答案 0 :(得分:0)
我已经对此进行了测试和测试,它可用于访问具有长子目录结构的文件,从而超出了Windows中的路径长度限制。
对于File.Exist()方法,可以使用以下替代方法:
if (!CheckPath(archiveDetail["downloadPath"]))
{
throw new Exception("The export file does not exist!");
}
public bool CheckPath(string path)
{
if (path.Length >= MAX_PATH)
{
return checkFile_LongPath(path);
}
else if (!File.Exists(path))
{
return false;
}
return false;
}
private bool checkFile_LongPath(string path)
{
string[] subpaths = path.Split('\\');
StringBuilder sbNewPath = new StringBuilder(subpaths[0]);
// Build longest subpath that is less than MAX_PATH characters
for (int i = 1; i < subpaths.Length; i++)
{
if (sbNewPath.Length + subpaths[i].Length >= MAX_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)
{
foundMatch = false;
// 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])
{
foundMatch = true;
break;
}
}
}
}
// If we didn't find a match, write to the console.
if (!foundMatch)
{
return false;
}
return foundMatch;
}
这是根据参考文献LongFileIssue
可以使用此blog
中提到的替代类文件LongPath解决读取文件的问题。但是在使用FileStream时,您必须指定文件名,因为LongPath的FileStream名称将不是必需的。