jharr100
jharr100

Reputation: 1469

Directory.GetFiles() throws exception - Folder name has spaces at the end not recognized

I have a folder name that looks like this:

\\server\pictures\607\27022015\HARD\  //temp dir
\\server\pictures\607\27022015\HARD\550528  //dir name (two spaces behind)
macId //a string representative of a folder name (two spaces behind)

In my code I get the folder name by doing this:

if (Directory.Exists(tempDirectoryWithoutMac))
                {
                    var subDirectories = Directory.GetDirectories(tempDirectoryWithoutMac);

                    var directoryToSearch = subDirectories.Where(c => c.Contains(macId.Trim()));

                    if (directoryToSearch.Any())
                    {
                        var newDirectory = directoryToSearch.First();

                        foreach (string filename in Directory.GetFiles(newDirectory))
                        {
                            var retVal = new BitmapImage(new Uri(filename.TrimEnd()));

                            return retVal;
                        }
                    }
                }

If I inspect and then copy and paste the newDirectory into Windows Explorer it takes me to the folder; however, my code throws an exception:

The network path was not found.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at URM.ViewModels.CollectionsViewModel.GetImages(String macId, String type)

If I try to rename the folder taking out the spaces at the end...Windows says...the source and destination file names are the same.

Edit I'm pasting this path (quotes used to show spaces):

"\\server\pictures\607\27022015\HARD\550528  "

Upvotes: 1

Views: 3335

Answers (3)

Wai Ha Lee
Wai Ha Lee

Reputation: 8815

It is possible to have characters which appear like spaces at the end of a file system entry (file/folder) using an alt code, specifically alt + 0160:

Using this trick, you can display a folder without a name.

...

If you right-click the folder, select Rename and enter only spaces, the operating system will not accept it.

To remove the name and display a blank name, right-click on the folder and select Rename. Now press the Alt key and from the Numeric keypad, press 0160.

The file system treats it not as a 'normal' space (alt + 0032) but as a 'no break space'. See also this link on answers.microsoft.com: "how do I add a space in front of a folder name?" (the asker wants the spaces at the start of the folder name so it appears first in an alphabetised list):

Actually, the "no break space" is ALT+0160...


A solution in Windows Explorer

I think that if you rename the folder, i.e. from "550528 " to a temporary name (e.g. "temp") then you will be able to rename it to "550528" (note the lack of spaces) successfully.


A solution in code

As others have said, if you enumerate through the file system entries in the containing folder, you'll get the entry with the correct special characters at the end without thinking about it.

Upvotes: 1

CalebB
CalebB

Reputation: 607

If you use DirectoryInfo and FileInfo Objects as appose to regular strings you will have much less whitespace issues to deal with and be able to bring about a much more object oriented, and reliable solution.

You can do this like the following:

DirectoryInfo tempWithoutMac = new DirectoryInfo(tempDirectoryWithoutMac);

DirectoryInfo MacID = new DirectoryInfo(macId);

DirectoryInfo wantedDir = tempWithoutMac.GetDirectories.Where(c => c.Contains(MacID)).First();

foreach (FileInfo file in wantedDir.GetFiles())

{
       var retVal = new BitmapImage(new Uri(file.fullname));

       return retVal;
}

This is just an example so you will need to tweak it to work for you but using this concept should help you out.

Cheers,

Upvotes: 2

Becuzz
Becuzz

Reputation: 6857

Windows Explorer strips the extra spaces at the end of the directory input. For example, try making a folder named "foo" somewhere, then try making a "foo " folder (with spaces at the end). Windows will complain that there is already a folder with that name. Just because the explorer program accepts it doesn't make it correct. Using spaces to make different folders is probably not the best idea.

Upvotes: 0

Related Questions