C#GetDirectories()System.IO.DirectoryNotFoundException:'找不到路径的一部分

时间:2018-10-30 09:56:43

标签: c#

我正在开发一个简单的File Explorer exe,该文件会导致此错误,我尝试了几种方法无法解决。

MyCode:

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 Male \ Cothing \ Super Bodysuit \ Materials \ Casual Style \ 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 Pro(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   库\数据\ DAZ3D-G3超级英雄包\ 24205-超级紧身衣裤休闲   样式   Textures \ IM00024205-01_SuperBodysuitCasualStyleTextures \ Content \ People \ Genesis   3男\服装\超级紧身衣裤\材料\休闲\ 3喜悦“

但是当我将此路径粘贴到Win10Pro的Windows资源管理器中时,它将自动将路径更改为较短的版本。可以正常工作,并显示文件夹及其内容。

  

Win10资源管理器:   “ C:\ Users \ zero_000 \ Documents \ DAZ3D〜1 \ Studio \ MYLIBR〜1 \ data \ DAZ3D-〜1 \ 24205-〜1 \ IM0002〜1 \ Content \ People \ GENESI〜1 \ Cothing \ 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 结果超长路径中。

即使文件路径很长,仍然可以解决此问题吗?

2 个答案:

答案 0 :(得分:1)

好的。我测试了一下之后。 我注意到对此没有直接的答案,因为整个问题是由于文件路径太长。

我测试了路径“ C:\ Users \ zero_000 \ Documents \ DAZ3D〜1 \ Studio \ MYLIBR〜1 \ data \ DAZ3D-〜1 \ 24205-〜1 \ IM0002〜1 \ Content \ People \ GENESI〜1 \ Cothing \ SUPERB〜1 \ MATERI〜1 \ CASUAL〜1 \ 3Delight ”,这是File Explorer生成的较短版本。

如果我导航到该文件夹​​,它将起作用。 但是,如果我尝试在此文件夹中创建一个文件夹,它只会向我抛出一个错误,类似于我的C#应用​​程序中显示的错误。

根据我的员工或任何用户的默认行为,这意味着他们甚至无法达到此阶段。 (除非像我一样,该文件夹是通过7zip提取的。)

我的选择是抛出一个警告消息框,以强制用户文件路径不长。

答案 1 :(得分:0)

您可以通过在Windows注册表中进行更改来在机器范围内进行这些- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation开关设置。这样,您不必手动将替代项添加到所有 App.config 文件中。

enter image description here

如果您需要在 .NET Framework 4.7 中将开关设置为“ true ”,则这可能特别相关,因为它具有known issue位置开关在某些解释不充分的情况下,这些值被单方面报告为“ false ”:

  

原因
当程序在注册表中读取AppContext开关的值时,无法检索数据。在这种情况下,假定注册表中指定设置的方式有问题,并且将False值返回给应用程序。

https://support.microsoft.com/en-us/help/4036977