TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# File.Delete

This C# example shows the File.Delete method from the System.IO namespace.

File.Delete. Deleting files is not always possible.

Sometimes documents are locked or unavailable. File.Delete throws an exception if this occurs. We look at exceptions related to the File.Delete method in the C# language targeting the .NET Framework.

Tip: Use File.Delete to delete files on the computer. This method will throw an exception in some cases.

Example. This program demonstrates one way to use the File.Delete method. We look at how you can catch the IOExceptions thrown by the File.Delete method and execute commands in this case. The type we want to detect is the System.IO.IOException.

IOException

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.

C# 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.

In part A, we try to delete a file. If the file is properly deleted or isn't there at all, then the method succeeds and returns true. In part B, we handle errors. We return false if something goes wrong and we catch an IOException.

Also: It is recommended that you always catch the most specific exception type possible.

Exception

Discussion. The File.Delete method will permanently delete a file, bypassing the recycle bin. You must be careful with exceptions—File.Delete does not throw an exception when a file doesn't exist. When exceptions are thrown, the file is locked.

File.Delete: 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 in the C# language. Never make any assumptions about the filesystem. Instead always check for errors and unexpected conditions.

Caution: File handling results in exceptions being thrown even in normal situations.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf