File with nbsp chars in name returned from Directory.GetFiles() but File.Exists() returns false

S

Samuel R. Neff

I have a file in a directory that is returned from
Directory.GetFiles() but subsequently fails a File.Exists() check.

Test code:

string p = @"C:\documents and settings\all users\" +
@"application data\macromedia\fms";
foreach(string path in Directory.GetFiles(p)) {
if ( ! File.Exists(path)) {
foreach(char c in Path.GetFileName(path)) {
Console.WriteLine((int)c);
}
}
}

prints out this:

160
160
160
32
160
160
160

That's the real directory, the one file in it was created by Flash
Media Server and is very small, just has some numbers in it followed
by a null-byte. In explorer the name looks like it's blank. I can
open it in other apps (Notepad), but in .NET I get
FileNotFoundException trying to access it.

Any explanation as to what .NET is doing here? This has got to be a
bug, right?

Working with .NET 2.0.

Thanks,

Sam
 
M

Morten Wennevik [C# MVP]

Samuel R. Neff said:
I have a file in a directory that is returned from
Directory.GetFiles() but subsequently fails a File.Exists() check.

Test code:

string p = @"C:\documents and settings\all users\" +
@"application data\macromedia\fms";
foreach(string path in Directory.GetFiles(p)) {
if ( ! File.Exists(path)) {
foreach(char c in Path.GetFileName(path)) {
Console.WriteLine((int)c);
}
}
}

prints out this:

160
160
160
32
160
160
160

That's the real directory, the one file in it was created by Flash
Media Server and is very small, just has some numbers in it followed
by a null-byte. In explorer the name looks like it's blank. I can
open it in other apps (Notepad), but in .NET I get
FileNotFoundException trying to access it.

Any explanation as to what .NET is doing here? This has got to be a
bug, right?

Working with .NET 2.0.

Thanks,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Hi Samuel,

I'm inclined to think this is a bug, although there may be a security
reasons behind. (char)160 and (char)32 are valid file characters, yet deep
inside System.IO the path is normalized with a string.TrimEnd(). Since
(char)160 and (char)32 are whitespace characters the resulting filename will
be blank.

I've submitted a bugreport for this you can follow to see if Microsoft
acknowledges it as a bu
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=373295
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top