static void Main()
// Call Delete wrapper method.
/// Wrap the Delete method with an exception handler.
static bool TryToDelete(string f)
// Try to delete the file.File.Delete(f);
// We could not delete the file.
}The file is deleted, or nothing happens.
Discussion. We must be careful with exceptions—File.Delete does not throw an exception when a file does not exist. When exceptions are thrown, the file is locked.
Detail Deletes the specified file. An exception is not thrown if the specified file does not exist.
Summary. We dealt with exceptions raised when trying to use the File.Delete method. Never make any assumptions about the file system—always check for errors and unexpected conditions.