I want to delete a file that may or may not exist. I write
try { File.Delete(temp); } catch { }
Is there an easier way? Also I realize this ignores other exceptions as well but i dont care in this case.
I want to delete a file that may or may not exist. I write
try { File.Delete(temp); } catch { }
Is there an easier way? Also I realize this ignores other exceptions as well but i dont care in this case.
try
{
if(File.Exists(temp))
File.Delete(temp);
}
catch(IOException e)
{
// file is in use
}
catch(UnauthorizedAccessException ex)
{
// read only
// no permissions
}
More can be found on the MSDN page for File.Delete
No need for the try catch
block to handle a non-existant file. The File.Delete
method does not throw an exception if the file does not exist. From MSDN:
If the file to be deleted does not exist, no exception is thrown.