3

A 3rd party library errors out when the file paths are invalid. We attempted to handle this case using File.Exists() thinking that it would return false when the file path contains invalid characters, but it returns true.

This is strange (see the extra spaces and period)

    string wrong = "myfolder1\\myfolder2\\myfile.txt      .";
    bool x = File.Exists(wrong);

Is there a way to clean up the file path?

new FileInfo(wrong).Name does not clean it up.

Our main purpose is to reliably determine if the file path is valid before sending it into the 3rd party library. I almost feel stupid asking this question because I think that File.Exists() ought to be doing this.

(We're on .NET 4.0)

101010
  • 14,866
  • 30
  • 95
  • 172
  • I would look into Regex.Replace: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx Static version: http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx – Nathan Jun 25 '13 at 14:47
  • I don't understand the question. You say: _"We have a file at path X. While the path X may look weird, the file exists visibly in Explorer at X and `File.Exists(X)` returns `true`. Now when we feed string X to a library where it expects a path, the library explodes"_. What exactly want you to do? Change a path that the system thinks is correct, in order to keep the library happy? Into what do you want to change the path then? – CodeCaster Jun 25 '13 at 14:52
  • I think I have just discovered a bug Windows Explorer: if I rename a file to its name + trailing dot, the file vanishes. But the file is actually still there, with the same name and it reappears after F5. – svick Jun 25 '13 at 14:53
  • _when the file path contains invalid characters_ Your example does not contain any illegal characters. ;-) – JustAnotherUserYouMayKnow Jun 25 '13 at 14:53
  • @JustAnotherUserYouMayKnow A period is an illegal character if it is the last character in the filename. – Scott Chamberlain Jun 25 '13 at 14:55
  • @CodeCaster As I understand it, there is no file called `myfile.txt .`. There is a file called `myfile.txt` and Windows treats the two names as if they were the same, but they are not same. – svick Jun 25 '13 at 14:55

1 Answers1

7

The windows API trims trailing periods and spaces from filenames, if it was anything other than a period it would have worked.

See this msdn article for more info.

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".


I don't know if it will work but you may be able to bypass the shell by using the \\?\ prefix, try File.Exists(@"\\?\C:\myfolder1\myfolder2\myfile.txt ."); and see if that works (I can't test this moment right now, if it does not let me know and I will remove this section.)

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 1
    @CodeCaster What is not true about the fact that the API of `File.Exists`is trimming trailing periods and spaces? This question is not about the creation of files, this is about testing for existence. Also the quote I put in says exactly what you said **"Although the underlying file system may support such names, the Windows shell and user interface does not."** File.Exists passes through the shell, and therfor trims the characters. – Scott Chamberlain Jun 25 '13 at 14:57
  • 1
    See also this question on whether or not a path is valid: http://stackoverflow.com/questions/6198392/check-whether-a-path-is-valid – plinth Jun 25 '13 at 15:42