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.

FileInfo.Delete Method

Permanently deletes a file.

[Visual Basic]
Overrides Public Sub Delete()
[C#]
public override void Delete();
[C++]
public: void Delete();
[JScript]
public override function Delete();

Exceptions

Exception Type Condition
IOException The target file is open or memory-mapped on a computer running Microsoft Windows NT.
SecurityException The caller does not have the required permission.
UnauthorizedAccessException The path is a directory.

Remarks

If the file does not exist, this method does nothing.

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

The following example demonstrates the Delete method.

[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 fi As FileInfo = New FileInfo(path)

        Try
            Dim sw As StreamWriter = fi.CreateText()
            sw.Close()
            Dim path2 As String = path + "temp"
            Dim fi2 As FileInfo = New FileInfo(path2)

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

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

            'Delete the newly created file.
            fi2.Delete()
            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";
        FileInfo fi1 = new FileInfo(path);

        try 
        {
            using (StreamWriter sw = fi1.CreateText()) {}
            string path2 = path + "temp";
            FileInfo fi2 = new FileInfo(path2);

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

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

            //Delete the newly created file.
            fi2.Delete();
            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";
    FileInfo* fi1 = new FileInfo(path);

    try {
        StreamWriter* sw = fi1->CreateText();
        if (sw) __try_cast<IDisposable*>(sw)->Dispose();

        String* path2 = String::Concat(path, S"temp");
        FileInfo* fi2 = new FileInfo(path2);

        //Ensure that the target does not exist.
        fi2->Delete();

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

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

The following example creates, closes, and deletes a file.

[Visual Basic] 
Imports System
Imports System.IO

Public Class DeleteTest
    Public Shared Sub Main()
        ' Create a reference to a file.
        Dim fi As New FileInfo("temp.txt")
        ' Actually create the file.
        Dim fs As FileStream = fi.Create()
        ' Modify the file as required, and then close the file.
        fs.Close()
        ' Delete the file.
        fi.Delete()
    End Sub 'Main
End Class 'DeleteTest

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

public class DeleteTest 
{
    public static void Main() 
    {
        // Create a reference to a file.
        FileInfo fi = new FileInfo("temp.txt");
        // Actually create the file.
        FileStream fs = fi.Create();
        // Modify the file as required, and then close the file.
        fs.Close();
        // Delete the file.
        fi.Delete();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Create a reference to a file.
    FileInfo* fi = new FileInfo(S"temp.txt");
    // Actually create the file.
    FileStream* fs = fi->Create();
    // Modify the file as required, and then close the file.
    fs->Close();
    // Delete the file.
    fi->Delete();
}

[JScript] 
import System;
import System.IO;

public class DeleteTest {
    public static function Main() : void {
        // Create a reference to a file.
        var fi : FileInfo = new FileInfo("temp.txt");
        // Actually create the file.
        var fs : FileStream = fi.Create();
        // Modify the file as required, and then close the file.
        fs.Close();
        // Delete the file.
        fi.Delete();
    }
}
DeleteTest.Main();

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

.NET Framework Security: 

See Also

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

Show:
© 2014 Microsoft