This documentation is archived and is not being maintained.

File.Delete Method

Deletes the specified file. An exception is not thrown if the specified file does not exist.

[Visual Basic]
Public Shared Sub Delete( _
   ByVal path As String _
public static void Delete(
 string path
public: static void Delete(
 String* path
public static function Delete(
   path : String


The name of the file to be deleted.


Exception Type Condition
UnauthorizedAccessException The caller does not have the required permission.


path is a directory.


path specified a read-only file.

ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException path is a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
IOException The specified file is in use.
UnauthorizedAccessException path is a directory.
NotSupportedException path is in an invalid format.


The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Copy a file. File.Copy


Rename or move a file. File.Move


Delete a directory. Directory.Delete


Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. CreateDirectory


See the files in a directory. Name
Set file attributes. SetAttributes

Windows NT 4.0 Platform Note:   Delete does not delete a file that is open for normal I/O or a file that is memory mapped.


[Visual Basic, C#, C++] The following example deletes a file from the specified path.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
            Dim sw As StreamWriter = File.CreateText(path)
            Dim path2 As String = path + "temp"

            ' Ensure that the target does not exist.

            ' Copy the file.
            File.Copy(path, path2)
            Console.WriteLine("{0} was copied to {1}.", path, path2)

            ' Delete the newly created file.
            Console.WriteLine("{0} was successfully deleted.", path2)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";
            using (StreamWriter sw = File.CreateText(path)) {}
            string path2 = path + "temp";

            // Ensure that the target does not exist.

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            Console.WriteLine("{0} was successfully deleted.", path2);
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    try {
        StreamWriter* sw = File::CreateText(path);
        if (sw) __try_cast<IDisposable*>(sw)->Dispose();
        String* path2 = String::Concat(path, S"temp");

        // Ensure that the target does not exist.

        // Copy the file.
        File::Copy(path, path2);
        Console::WriteLine(S"{0} was copied to {1}.", path, path2);

        // Delete the newly created file.
        Console::WriteLine(S"{0} was successfully deleted.", path2);
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File