'Check if file exist with Try/Catch

I'm new to this about Try/Catch. In the code below I have a simple test to check if a file exist. In my task for my C# lesson I must use Try/Catch, and I'm not sure how to use this, should I still use the if statement inside the Try part or is there a better way to do the checking if a file exist inside Try? Is there any difference if the file is a simple txt file or a serialized file?

if (File.Exists("TextFile1.txt"))
{
   MessageBox.Show("The file don't exist!", "Problems!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

The Try/Catch way I must use

try
{
code to check if file exist here
}
catch
{
error message here
}


Solution 1:[1]

try
{
 if (!File.Exists("TextFile1.txt"))
    throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
   // your message here.
}

Solution 2:[2]

Try this :

try
{
   if(!File.Exist("FilePath"))
       throw new FileNotFoundException();

   //The reste of the code
}
catch (FileNotFoundException)
{
    MessageBox.Show("The file is not found in the specified location");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Solution 3:[3]

If you want to check if the file exists without using File.Exist, then you may try opening the file in a try block, and then catching the exception FileNotFoundException.

try
{
    // Read in non-existent file.
    using (StreamReader reader = new StreamReader("TextFile1.txt"))
    {
    reader.Read();
    }
}
catch (FileNotFoundException ex)
{
    MessageBox.Show("The file don't exist!", "Problems!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    // Write error.
    Console.WriteLine(ex);
}

Solution 4:[4]

Use throw:

try
{
    if (!File.Exists("TextFile1.txt"))
        throw (new Exception("The file don't exist!"));
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Solution 5:[5]

You only use a try/catch, when you have an unexpected error, or you are "expecting" an error from accessing a resource. That sounds a bit confusing, but in your cause, there is neither.

If however you opened as stream to READ a file without checking if it exists, that will then be necessary to have a try..catch.

All in all, try..catch's should be applied for safety, and where code is complex/length.

Solution 6:[6]

Related to this question following thread will be interesting

http://social.msdn.microsoft.com/Forums/en-NZ/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb

File.Exists API was changed in window 8 and explaination is:

Currently the only way to check if a file exists is to catch the FileNotFoundException. As has been pointed out having an explicit check and the opening is a race condition and as such I don't expect there to be any file exists API's added. I believe the File IO team (I'm not on that team so I don't know for sure but this is what I've heard) is considering having this API return null instead of throwing if the file doesn't exist.

Approach with File.Exists is not thread safe and it was removed from API in windows 8. So just catch FileNotFoundException

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AksharRoop
Solution 2
Solution 3
Solution 4
Solution 5 Dane Balia
Solution 6 Regfor