0

In some part of my code, I check if a file exists and then I open it.

One employee encountered a problem with filenames containing more than one space character.

I checked and it's true. Here's a snippet of my code:

string filePath = Path.Combine(helper.MillTestReportPath, fileName);

// Ouverture du fichier
if (File.Exists(filePath))
{
    Process.Start(filePath);
}
else
{
    MessageBox.Show("Le fichier n'existe pas!", "Fichier introuvable", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Everything works just find with almost every file but when a file ("SPAM CERTS S O 94318099   P O 10610.msg" for example) contains more than one space, I get false with File.Exists and even if I directly try to run Process.Start it fails...

Any idea about how I could fix that?

Thanks a lot!

nickgrim
  • 5,243
  • 1
  • 20
  • 26
Tommy B.
  • 3,257
  • 12
  • 54
  • 97
  • 5
    One suggestion is to prefer `Path.Combine()` over `String.Concat()`. – Henk Holterman Feb 18 '11 at 15:38
  • It's something else, tried now to open `my file with spaces.txt` with `Process.Start` and it works just fine. Debug and check what is the value of `filePath` variable. – Shadow The Vaccinated Wizard Feb 18 '11 at 15:39
  • @Shadow: I said with more than ONE spaces. Files with single spaces are just fine. – Tommy B. Feb 18 '11 at 15:43
  • @Henk : thanks for the suggestion! – Tommy B. Feb 18 '11 at 15:44
  • I don't think it is the number of spaces that cause problems. Try getting byte arrays from the filename and see what characters they are. Check this thread if you need help with string to byte array: http://stackoverflow.com/questions/472906/net-string-to-byte-array-c – Holystream Feb 18 '11 at 16:13
  • 4
    @Tom the file name `my file with spaces.txt` contains THREE spaces. Feel free to count yourself if you don't believe me. – Shadow The Vaccinated Wizard Feb 19 '11 at 09:16
  • 2
    @Shadow: I meant: three spaces IN A ROW not in the total filename... argh – Tommy B. Feb 20 '11 at 20:31
  • @Tom: Might wanna update your example, then. "SPAM CERTS S O 94318099 P O 10610.msg" doesn't contain three spaces in a row. – rossisdead Mar 02 '11 at 21:20
  • It did in the source; I added nonbreaking space chars. – Dour High Arch Mar 02 '11 at 22:09
  • Perhaps its related to language settings in the OS? We've seen different behavior with paths for some of our clients when the language on the machine is set to French vs. English. – carmbrester Mar 02 '11 at 22:43
  • I tried this using the original filename and a French OS. Worked fine. Either there are odd characters in the file name or perhaps there is an permissions issue. – Thomas Mar 05 '11 at 18:28
  • @Tom, you said `"R:\\Nesting\\Mill Test Report\\...` in one of your comments.. is this a web based or desktop programme? What's the file system format of the R: drive? Through what technology is R: shared/mounted (Windows, Novell etc) How are you getting the value of `fileName` - are you sure it absolutely matches the value of the stored filename? (spaces and/or special characters, zeros and ohs in the right places?) – Rudu Mar 08 '11 at 21:29
  • @Tom did you find a solution for this? – oleschri May 31 '11 at 10:35

7 Answers7

5

According to MSDN documentation, File.Exists returns:

true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

If the file exists, then probably the user that is trying to access the file does not have necessary permissions.

Rest Wing
  • 2,650
  • 1
  • 21
  • 27
2

I suspect your filename(s) do not only contain ANSI space characters (char)32 0020hex but other ANSI characters that are indistinguishable from space characters.

If your files reside on an NTFS drive, file names can even contain Unicode characters.

I wrote a small PowerShell script that shows you the filenames of the current folder in hex

dir | % {
    $chars = $_.Name.ToCharArray(); """$($_.Name)""";
    $result = "|";
    foreach ($char in $chars) {
        $result += [String]::Format(" {0}  |",$char)
    };
    "$result";
        $result = "|"
        foreach ($char in $chars) {
            $hexChar = [System.Convert]::ToInt32($char);
            $result += $hexChar.ToString("x4");
            $result += "|";
            };
        "$result`r`n";
    }

Typical output is

"1000 €.txt"
| 1  | 0  | 0  | 0  |    | €  | .  | t  | x  | t  |
|0031|0030|0030|0030|0020|20ac|002e|0074|0078|0074|

"A normal file.txt"
| A  |    | n  | o  | r  | m  | a  | l  |    | f  | i  | l  | e  | .  | t  | x  | t  |
|0041|0020|006e|006f|0072|006d|0061|006c|0020|0066|0069|006c|0065|002e|0074|0078|0074|

"what the ңёςк.txt"
| w  | h  | a  | t  |    | t  | h  | e  |    | ң  | ё  | ς  | к  | .  | t  | x  | t  |
|0077|0068|0061|0074|0020|0074|0068|0065|0020|04a3|0451|03c2|043a|002e|0074|0078|0074|

        etc.

You can see real ANSI spaces as 0020hex here.

oleschri
  • 1,983
  • 10
  • 20
0

The number of spaces should not be a problem. Did you check the output string of filePath? I'm sure it will not be right. As Henk suggested, if the output is not correct try to change to Path.Combine().

Bonne journée

Mathieu
  • 4,233
  • 6
  • 37
  • 58
  • Thanks for your answer, I changed to Path.Combine and it is still not working. The output string is okay I guess: "R:\\Nesting\\Mill Test Report\\SPAM CERTS S O 94414834 P O 11304.msg" What do you think? – Tommy B. Feb 18 '11 at 16:00
  • I'd suggest using @"yourpath" instead of the escape character, it's safer and would give you more chances – Mathieu Feb 19 '11 at 04:12
  • @Tom copy the exact path while debugging and try browsing to it directly from Windows Explorer - can you access the file this way? – Shadow The Vaccinated Wizard Feb 21 '11 at 08:07
0

Ran this test code using the same filename you've specified:

        const string path = @"C:\TEMP\SPAM CERTS S O 94318099   P O 10610.msg";
        if (File.Exists(path)) {
            Trace.WriteLine("EXIST");
            Process.Start(path);
        }
        else {
            Trace.WriteLine("NOT EXIST");
        }

The file is correctly found to exist even with multiple sequential spaces, etc. It also successfully launches the associated program (Noteapad++ in my case).

I suspect, as others indicate, that your problem is elsewhere. What is the failure you are seeing with Process.Start?

g01d
  • 501
  • 4
  • 18
0

What happens when you try this with that file path:

  try {
    string path = Path.Combine(helper.MillTestReportPath, fileName);
    using (FileStream fs = File.Open(path, FileMode.CreateNew)) {
    }
  } catch (IOException ex) {
    // any exception here?
  }
Luke Hutton
  • 9,906
  • 6
  • 28
  • 54
0

I think others have already covered the File related stuff I would check but have you considered localization/encoding in your check string vs the local file system?

It appears you are using ?German? in your message box prompt, might be comparing apples to oranges... just a thought.

Perry
  • 528
  • 2
  • 11
  • Comment from carmbrester is regarding the same thing, comment was hidden and just noticed it so they should get credit if correct :) – Perry Mar 03 '11 at 14:02
0

Hey. Please list the parent dir programmatically. And then for each child file echo all the filenames char-by-char. Preferrably in hex or unicode codes.

You're probably having some non-trivial whitespace character somewhere in the file name. Especially if the filename was generated automatically from some keystore or xml file.

Anton K
  • 3,796
  • 4
  • 23
  • 37