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?