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.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

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 typeCondition


path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.


path is a null reference (Nothing in Visual Basic).


The specified path is invalid, (for example, it is on an unmapped drive).


The specified file is in use.


path is in an invalid format.


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.


The caller does not have the required permission.


path is a directory.


path specified a read-only file.

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

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File



Copy a file.



Rename or move a file.



Delete a directory.



Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Create a directory.



See the files in a directory.


Set file attributes.


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.

The following example deletes a file from the specified path.

using namespace System;
using namespace System::IO;
int main()
   String^ path = "c:\\temp\\MyTest.txt";
      StreamWriter^ sw = File::CreateText( path );
      if ( sw )
            delete (IDisposable^)sw;

      String^ path2 = String::Concat( 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 );

import System.*;
import System.IO.*;

class Test
    public static void main(String[] args)
        String path = "c:\\temp\\MyTest.txt";

        try {    
            StreamWriter sw = File.CreateText(path);
            try {
            finally {
            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 (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0