C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The metadata says that this indicates that "The specified file is in use."
Tip: We can wrap the Delete call in another method that handles some of the errors.
Part A: We invoke File.Delete here. If the file is properly deleted or is not present, then the method succeeds and returns true.
Part B: Here we handle errors. We return false if something goes wrong and we catch an IOException.
CatchExceptionBool MethodC# program that catches IOException
using System.IO;
class Program
{
    static void Main()
    {
        // 1.
        // Call Delete wrapper method.
        TryToDelete("Word.doc");
    }
    /// <summary>
    /// Wrap the Delete method with an exception handler.
    /// </summary>
    static bool TryToDelete(string f)
    {
        try
        {
            // A.
            // Try to delete the file.
            File.Delete(f);
            return true;
        }
        catch (IOException)
        {
            // B.
            // We could not delete the file.
            return false;
        }
    }
}
Output
The file is deleted, or nothing happens.
File.Delete: Deletes the specified file. An exception is not thrown if the specified file does not exist.
Caution: File handling results in exceptions being thrown even in normal situations.