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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samuel R. Neff

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

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

    Test code:

    string p = @"C:\documen ts and settings\all users\" +
    @"applicatio n data\macromedia \fms";
    foreach(string path in Directory.GetFi les(p)) {
    if ( ! File.Exists(pat h)) {
    foreach(char c in Path.GetFileNam e(path)) {
    Console.WriteLi ne((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
    FileNotFoundExc eption 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.
  • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

    #2
    RE: File with nbsp chars in name returned from Directory.GetFi les() bu


    "Samuel R. Neff" wrote:
    I have a file in a directory that is returned from
    Directory.GetFi les() but subsequently fails a File.Exists() check.
    >
    Test code:
    >
    string p = @"C:\documen ts and settings\all users\" +
    @"applicatio n data\macromedia \fms";
    foreach(string path in Directory.GetFi les(p)) {
    if ( ! File.Exists(pat h)) {
    foreach(char c in Path.GetFileNam e(path)) {
    Console.WriteLi ne((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
    FileNotFoundExc eption 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 bug


    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    Working...