3D-kreativ
3D-kreativ

Reputation: 9301

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
}

Upvotes: 3

Views: 45157

Answers (7)

Habib
Habib

Reputation: 223207

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);
}

Upvotes: 6

Ria
Ria

Reputation: 10367

Use throw:

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

Upvotes: 2

SidAhmed
SidAhmed

Reputation: 2352

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);
}

Upvotes: 7

Regfor
Regfor

Reputation: 8091

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

Upvotes: 0

AksharRoop
AksharRoop

Reputation: 2293

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

Upvotes: 11

Dane Balia
Dane Balia

Reputation: 5367

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.

Upvotes: 0

Tigran
Tigran

Reputation: 62248

You already check on the presence of file in your first sniplet. There is no any need, for this code to be inside try/catch block.

Upvotes: 2

Related Questions