6

For the file C:\filename.txt,

File.Exists(@"c:\filename.txt/"); 

returns true. But when we then try to open with that filename, it fails.

The filename is bad because of the trailing /. But how can we test to know if a file is valid as this tells me it is not only ok, but that the file exists.

Update: In a standard command line test app the results are as expected (false). But in my custom uri handler the File.Exists() returns true. It's really bizarre.

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • 5
    For me it returns false in Linqpad – Sriram Sakthivel Nov 12 '13 at 18:04
  • Maybe this question can help you: http://stackoverflow.com/questions/62771/how-check-if-given-string-is-legal-allowed-file-name-under-windows – xsl Nov 12 '13 at 18:04
  • 3
    I reach the same conclusion as @Sriram. What kind of framework are you running, .NET, Windows Store, anything else? – Anders Gustafsson Nov 12 '13 at 18:26
  • I am getting a false in LinqPad as well. – abhi Nov 12 '13 at 20:30
  • 3
    You're going to need to post more of your code. Running this in a basic WinForms C# project, it returns false with the trailing slash (but true without it, assuming I've created the file first). I can't get it to return true with the trailing slash. I'd like to see how you did it. – Ryan Lundy Nov 12 '13 at 20:42
  • 1
    Please show the code of your custom handler so we can recreate the results. You need to give us a [SSCCE](http://sscce.org/) to re-create the problem or your question will not get any useful answers. I am upvoting because this is a good question, but I have to vote to close as the question is un-answerable without a SSCCE. – Scott Chamberlain Nov 12 '13 at 21:19
  • @ScottChamberlain The problem is the SSCCE I created works as expected. It only has this problem when the code is called as a custom UI handler and you can't set that up as an SSCCE (I think). – David Thielen Nov 13 '13 at 16:13
  • I would still try my best to create one and include the steps you do to re-create the problem in the custom UI handler. Without steps for us to re-create a custom UI Handler that behaves the same as yours I don't think anyone will be able to answer your question. – Scott Chamberlain Nov 13 '13 at 16:17

1 Answers1

1

Usually, when querying a system folder, file visualization is the culprit in this kind of issue. IE the file exists at the virtual store location but not in the actual queried path. As a result, attempting to open it will fail. So, before you state that the file doesn't exist. . . you should make sure it really doesn't exist.

iheanyi
  • 3,107
  • 2
  • 23
  • 22