Export (0) Print
Expand All

File.Delete Method

Updated: July 2009

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

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

public static void Delete(
	string path
)

Parameters

path
Type: System.String

The name of the file to be deleted.

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

DirectoryNotFoundException

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

IOException

The specified file is in use.

NotSupportedException

path is in an invalid format.

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.

UnauthorizedAccessException

The caller does not have the required permission.

-or-

path is a directory.

-or-

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 a list of common I/O tasks, see Common I/O Tasks.

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.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: On Windows Mobile 6.0, you may experience I/O failures when trying to delete an .mp3 file, a .wma file, or a file without an extension. This occurs because voicecmd.exe on Windows Mobile 6.0 opens handles to these file types for indexing purposes.

The following example copies groups of files to the C:\archives\2008 backup folder and then deletes them from the source folder.

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files. 
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path. 
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files. 
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied. 
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied. 
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

July 2009

Replaced the code example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft