The best way, I suppose, is to use File.Exists before trying to create a FileStream. Example:
if (File.Exists(path)) { //Handle existing file } else { FileStream newFile = new FileStream(path, FileMode.CreateNew); //Logic to do with your new file }
Trying to parse an IOException to make sure that the βcorrectβ IOException is fragile. I believe the only way to do this is to compare a comment or description of an IOException.
Daryl source share