How to detect System.IO.IOException cause by existing file?
asp.net, c#, exception, ioexception
Solution
You should avoid ever making logic decisions based on the contents of the Exception.Message property. That is defined to be a human-readable message, not something for a program to read. Such messages are subject to change without notice.
Human-readable messages often change to make them more readable to humans. That won't make your program any happier.
As you might be able to tell, the issue of program logic depending on human-readable messages is a pet peeve of mine - I won't tell you for how long, since that would make me feel old. This has distracted me from something that should have been obvious.
Please check to see if you get a System.IO.FileNotFoundException when the file does not exist. Try catching that instead of IOException.
Problem
I want to create and open a file but only if it doesnt exist. I dont want to use a File.Exists because a thread by switch after it creating a file with the same name. How do i check if the exception System.IO.IOException was caused by the file existing? I prefer not to parse the error msg (even tho it can be as simple as .indexOf("exist")) How should i do this?