C# GetDirectories() System.IO.DirectoryNotFoundException: 'Could not find a part of the path

2

I'm working on a simple File Explorer exe which leads to this error and I tried a few methods and couldn't solve it.

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);
    }
}

But then I get this error:

System.IO.DirectoryNotFoundException HResult=0x80070003
Message=Could not find a part of the path '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'.
Source=mscorlib

I have tried shorten the file path using:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern int GetShortPathName(
        [MarshalAs(UnmanagedType.LPTStr)]
        string path,
        [MarshalAs(UnmanagedType.LPTStr)]
        StringBuilder shortPath,
        int shortPathLength
    );

But this just leads to another problem which is essentially the same.

Anyway to resolve this?

[Update 1] The program is supposed to look into every files and folders i have, and sometimes the names are long and they aren't created by me, but by the installer or some other user's zip file naming.

Here's some screenshot as requested.

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

I'm running on:

  • Win 10 Pro (64 bit)
  • VS2017
  • .NET Framework 4.7.1

My 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>

[Update 2] Okay,some of you might not get what I'm trying to show here. Basically here's the problem, the C# Desktop App i'm coding throws me an error suspect due to long file path.

But here's what's funny. In my c# code, the nested function to .GetDirectories() keeps going deeper and deeper into the subfolders. Hence it produced a long file path. But it throws and error shown above.

VS.NET 2017: "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"

But when I paste this path into Windows's Explorer in Win10Pro, it automatically changes the path into shorter version. Which works and it shows the folder and it's content.

Win10 Explorer: "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"

Then I pasted the same path into CMD and it shows error as well by saying the path doesn't exist (As shown in the screenshot above).

[Update 3] I thought I should check again on the most simplest method to see if it was really it by doing:

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");

It doesn't seems to be generating any errors in this case.

[Update 4] Apparently, if i do da.GetDirectories(); it will throw error instantly because any directories collected from that path will be added into the DirectoryInfo resulted super long path.

Is there anyway to resolve this even if it's long file path?

c#
asked on Stack Overflow Oct 30, 2018 by Zero Khor • edited Oct 31, 2018 by Zero Khor

2 Answers

2

Ok. After I've tested out a bit. I noticed there are no straight answer to this because the whole issue is due to long file path.

I tested the path "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" which is the shorter version generated by File Explorer.

If I navigate to this folder, it works. But if I tries to create a folder inside this folder, it will just throw an error to me somewhat similar to the errors shown in my C# App.

Which means, given by the default behavior of my staff or any users, they wouldn't even be able to reach this stage. (unless like me, this folder was extracted via 7zip).

My option is to just throw a warning message box to force user not to have long file path.

answered on Stack Overflow Oct 31, 2018 by Zero Khor
0

You can make those AppContext switch settings machine-wide by putting the change in the Windows registry. This way, you don't have to manually add the overrides to all of your App.config files.

enter image description here

This could be especially relevant if you need to set a switch to 'true' in .NET Framework 4.7, because it has a known issue where switch values are unilaterally reported as 'false' under certain poorly explained circumstances:

Cause
When the program reads the value of an AppContext switch in the registry, the data cannot be retrieved. In this case, it is assumed that something is wrong with the way the setting is specified in the registry, and a value of False is returned to the application.

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

answered on Stack Overflow Nov 23, 2018 by Glenn Slayden

User contributions licensed under CC BY-SA 3.0