string directorypath = @"C:\Folder01\Subfolder01\Next_level_Down ";
DirectoryInfo mydirectoryinfo = new DirectoryInfo(directorypath);
When the above code is run mydirectoryinfo.FullName and FullPath will cut off the trailing white space at the end of the last folder in directorypath. This seems like a bug?
This will cause a crash when I run:
DirectoryInfo[] mysubdirectories = mydirectoryinfo.GetDirectories();
As it will throw an exception "could not find a part of the path..."
I have a bunch of old folders that I am sorting and collecting data on so I need to get the DirectoryInfo on them, but some of them have been saved with white space at the end of the folder name. Rather than do a separate pass to correct folder names (which could break other connections to these folders) I was hoping on being able to get DirectoryInfo to handle the white space at the end of the folder name. If that is possible?
I have a list of directories that I've entered into a .CSV file. The paths are read into a List before being looped through to instance DirectoryInfo and run my checks, so there is no way to add more formatting that might help. I am using .Net Framework 4.7.2
CodePudding user response:
I don’t know how you did create a Folder with a white space at the and but if it is possible add a backslash
string directorypath = @"C:\Folder01\Subfolder01\Next_level_Down \";
DirectoryInfo mydirectoryinfo = new DirectoryInfo(directorypath);
If your real path looks like the code above then delete the whitespace or the backslash. I think it is not possible to create a directory with a whitespace at the end and also why to put there a white space? The last possible solution for your problem, that comes to my mind, is that the path is wrong. Somewhere in the middle is a character not correct, so that this exception will be thrown. I hope i could help you
CodePudding user response:
What also helps with such unorthodox path names not supported by the shell (see https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/file-folder-name-whitespace-characters) is prepending \\?\
:
string directorypath = @"\\?\C:\Folder01\Subfolder01\Next_level_Down ";
DirectoryInfo mydirectoryinfo = new DirectoryInfo(directorypath);