Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 _
)
[C#]
public static void Delete(
 string path
);
[C++]
public: static void Delete(
 String* path
);
[JScript]
public static function Delete(
   path : String
);

Parameters

path
The name of the file to be deleted.

Exceptions

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

-or-

path is a directory.

-or-

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.

Remarks

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

File.AppendText

FileInfo.AppendText

Copy a file. File.Copy

FileInfo.CopyTo

Rename or move a file. File.Move

FileInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.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

Directory

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.

Example

[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"
        Try
            Dim sw As StreamWriter = File.CreateText(path)
            sw.Close()
            Dim path2 As String = path + "temp"

            ' Ensure that the target does not exist.
            File.Delete(path2)

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

            ' Delete the newly created file.
            File.Delete(path2)
            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

[C#] 
using System;
using System.IO;

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

            // Ensure that the target does not exist.
            File.Delete(path2);

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

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

[C++] 
#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.
        File::Delete(path2);

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

        // Delete the newly created file.
        File::Delete(path2);
        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.

Requirements

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

Show:
© 2014 Microsoft