I am using a using with the BinaryWriter as follows:
using (BinaryWriter writer = new BinaryWriter(File.Open(configData.RxOutFn, FileMode.Create)))
{
// Do Something
}
Sometimes I have the file open in another app, and the Create fails, how can I catch this error?
I tried putting a try/catch around the whole thing like this:
try
{
using (BinaryWriter writer = new BinaryWriter(File.Open(configData.RxOutFn, FileMode.Create)))
{
// Do something
}
}
catch
{
// Display error
}
But I am worried it will catch the wrong thing as there is lots of code in the
// Do Something
Any ideas how i can catch this error?
Many thanks in advance
Andy
Check what exception gets thrown (
File.Open
throws anIOException
which I think is what gets thrown when the file can't be created) and catch that specific exception: