user34537
user34537

Reputation:

How to detect System.IO.IOException cause by existing file?

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?

Upvotes: 5

Views: 9224

Answers (5)

John Saunders
John Saunders

Reputation: 161831

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.

Upvotes: 5

JaredPar
JaredPar

Reputation: 755587

Your best option here is to use the CreateNew option when opening the file for writing.

There is simple no reliable way to check for the existence of a file on disk. The only thing you can check for is whether or not a file used to and may still exist on disk to which the process has at least a limited form of access.

Even if you gate all access to a file from your application, you cannot reliably prevent some other application from creating / deleting the file. Even with sufficient file system level locks, users can do nefarious things like take a USB key out of the computer.

The best way to approach this type of problem is to use open options like CreateNew. This will allow the operation to only succeed if the file is not in existence at the point in time the creation is attempted. You can catch the exception at this point and attempt to infer if it was from the file existing or some other invalid access exception.

Methods like File.Exist give a false sense of security to your code base and should be closely reviewed on check in.

Upvotes: 3

JP Alioto
JP Alioto

Reputation: 45127

You can use File.Open and call it with FileMode.OpenOrCreate.

Per comment: Okay, how about FileMode.CreateNew? Then, if there is one already, you will get an IOException, otherwise, make a new one.

Upvotes: 0

jerryjvl
jerryjvl

Reputation: 20151

I'm not sure he wants to open the file at all if it already exists.

I think he is actually after FileMode.CreateNew this will throw an IOException if the file already exists, but otherwise will create and open it.

Upvotes: 5

Simon_Weaver
Simon_Weaver

Reputation: 146218

You could always check File.Exists()... from WITHIN the exception catch block. You don't want to check before because of threading - so check after you already know you have a problem.

Assuming you're not suddenly deleting the file from a different thread this would be an easy and obvious way of doing it.

Don't forget that the File.Exists may itself cause an exception so be sure to catch it again.

Upvotes: 3

Related Questions