【发布时间】:2018-11-23 09:28:13
【问题描述】:
我正在开发一个导致此错误的简单文件资源管理器 exe,我尝试了几种方法都无法解决。
我的代码:
private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
然后我得到这个错误:
System.IO.DirectoryNotFoundException HResult=0x80070003
Message=找不到路径的一部分 'C:\Users\zero_000\Documents\DAZ 3D\Studio\My Library\data\DAZ3D - G3 超级英雄包\24205 - 超级紧身衣休闲风格 Textures\IM00024205-01_SuperBodysuitCasualStyleTextures\Content\People\Genesis 3 男\服装\超级紧身衣\材质\休闲风\3Delight'。
来源=mscorlib
我尝试使用以下方法缩短文件路径:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);
但这只会导致另一个本质上相同的问题。
无论如何要解决这个问题?
[更新 1] 该程序应该查看我拥有的每个文件和文件夹,有时名称很长,它们不是由我创建的,而是由安装程序或其他用户的 zip 文件命名创建的。
根据要求,这是一些屏幕截图。
Screenshot: The error coming from my IDE
Screenshot: The folder that exist when i open from my Explorer
Screenshot: The path that seems to be not found when I access from CMD
我正在跑步:
- Win 10 专业版(64 位)
- VS2017
- .NET Framework 4.7.1
我的 App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
</startup>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>
</configuration>
[更新 2] 好的,你们中的一些人可能不明白我要在这里展示的内容。 基本上这就是问题所在,我正在编码的 C# 桌面应用程序由于文件路径长而使我怀疑错误。
但有趣的是。 在我的 c# 代码中,.GetDirectories() 的嵌套函数不断深入到子文件夹中。因此它产生了一个长文件路径。但它会抛出上面显示的错误。
VS.NET 2017:“C:\Users\zero_000\Documents\DAZ 3D\Studio\My Library\data\DAZ3D - G3 Superhero Pack\24205 - 超级紧身衣休闲 风格 Textures\IM00024205-01_SuperBodysuitCasualStyleTextures\Content\People\Genesis 3男\服装\超紧身衣\材质\休闲风\3Delight”
但是当我将此路径粘贴到 Win10Pro 中的 Windows 资源管理器中时,它会自动将路径更改为更短的版本。哪个有效,它显示文件夹及其内容。
Win10 资源管理器: "C:\Users\zero_000\Documents\DAZ3D~1\Studio\MYLIBR~1\data\DAZ3D-~1\24205-~1\IM0002~1\Content\People\GENESI~1\Clothing\SUPERB~1\ MATERI~1\CASUAL~1\3Delight"
然后我将相同的路径粘贴到 CMD 中,它也显示错误,说路径不存在(如上面的屏幕截图所示)。
[更新 3] 我想我应该再次检查最简单的方法,看看是否真的是这样:
DirectoryInfo da = new DirectoryInfo(@"C:\Users\zero_000\Documents\DAZ3D~1\Studio\MYLIBR~1\data\DAZ3D-~1\24205-~1\IM0002~1\Content\People\GENESI~1\Clothing\SUPERB~1\MATERI~1\CASUAL~1\3Delight");
Console.WriteLine(@"C:\Users\zero_000\Documents\DAZ3D~1\Studio\MYLIBR~1\data\DAZ3D-~1\24205-~1\IM0002~1\Content\People\GENESI~1\Clothing\SUPERB~1\MATERI~1\CASUAL~1\3Delight");
DirectoryInfo db = new DirectoryInfo(@"C:\Users\zero_000\Documents\DAZ 3D\Studio\My Library\data\DAZ3D - G3 Superhero Pack\24205 - Super Bodysuit Casual Style Textures\IM00024205-01_SuperBodysuitCasualStyleTextures\Content\People\Genesis 3 Male\Clothing\Super Bodysuit\Materials\Casual Style\3Delight");
Console.WriteLine(@"C:\Users\zero_000\Documents\DAZ 3D\Studio\My Library\data\DAZ3D - G3 Superhero Pack\24205 - Super Bodysuit Casual Style Textures\IM00024205-01_SuperBodysuitCasualStyleTextures\Content\People\Genesis 3 Male\Clothing\Super Bodysuit\Materials\Casual Style\3Delight");
在这种情况下似乎没有产生任何错误。
[更新 4] 显然,如果我这样做 da.GetDirectories(); 它会立即抛出错误,因为从该路径收集的任何目录都将添加到 DirectoryInfo 导致的超长路径中。 p>
即使文件路径很长,有没有办法解决这个问题?
【问题讨论】:
-
你是如何调用你的函数的?你给它一个原始字符串吗?否则尝试使用 '\\' 而不是 '\'。
-
请更新您的问题以包含一个命令提示符的屏幕截图,其中
cded 到该文件夹中。换句话说,请向我们证明该文件夹存在。 -
路径看起来很长,这可能是问题的原因。有几种方法可以处理这个问题。可以试试this answer中的一些建议吗?
-
这是一个路径字符串。它超过了 MAX_DIR,winapi 中的 254 个字符。并且总是 MAX_PATH,259 个字符。当您尝试访问该目录中的文件时。当然,有很多方法可以通过选择更好的名称来使这个字符串更实用。
-
这些屏幕截图都不能证明具有该确切名称的文件夹存在。
标签: c#