DeleteFile (kernel32)
Last changed: -212.44.20.206

.
Summary

C# Signature:

[DllImport("kernel32.dll"), SetLastError=true]
static extern bool DeleteFile(string lpFileName);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

You can delete Alternate Data Streams (ADS). For example, to delete the ZoneId from a file:

DeleteFile(filename + ":Zone.Identifier");

N.B. You can't delete ADS using the managed APIs.

Sample Code:

Always check the return value as false indicates failure. You can throw the appropriate .NET exception automatically by doing the following:

if(!DeleteFile(filename)) {
    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

Alternative Managed API:

System.IO.File:

public static void Delete(string path);

Documentation
DeleteFile on MSDN