I am trying to iterate over files in a local directory:
foreach (string name in Directory.GetFileSystemEntries(path))
{
FileAttrtibutes att = File.GetAttributes(name)
}
One of the files in the folder at path
is named "This is a test". GetAttributes()
throws an exception on filenames with spaces.
Apart from replacing spaces with some other character, how should I handle spaces in filenames in this circumstance?
I apologise. I was a bit hasty. Let me redefine the problem, even though I now have a workaround at least.
This snippet of code shows the problem:
Uri u = new Uri(@"file:///c:/test/This is a test");
FileAttributes a = File.GetAttributes(u.AbsolutePath);
File.GetAttributes throws a System.IO.FileNotFoundException: Could not find file 'c:\test\This%20is%20a%20test'. 'c:\test\This is a test' exists.
So it seems Uri.AbsolutePath is inserting %20 for space, and I can just do a string replace to get my code working. I don't know that I should expect to have to do the replace, but at least I can get it working. Any other ideas welcome.