How to detect the cause of a System.IO.IOException by an existing file?

I want to create and open a file, but only if it does not exist. I do not want to use File.Exists, because the stream through the switch after creating the file with the same name.

How to check if a System.IO.IOException was raised by an existing file? I prefer not to parse the msg error (even if it can be as simple as .indexOf ("exists"))

How can I do it?

+5
source share
5 answers

Exception.Message. - , - . .

, . .


, - - , , . , .

, , System.IO.FileNotFoundException, . IOException.

+5

, , .

, FileMode.CreateNew, IOException, , .

+6

File.Exists()... ININ catch catch. - - , , .

, , .

, File.Exists , .

+3

- CreateNew .

. , , , , , , .

, - . , , USB- .

- , CreateNew. , , . , - .

, File.Exist, .

+3

File.Open FileMode.OpenOrCreate.

: , FileMode.CreateNew? , , IOException, .

0
source

All Articles