DeleteFile (kernel32)
Last changed: -117.254.108.28

.
Summary

C# Signature:

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

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFileA([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFileW([MarshalAs(UnmanagedType.LPWStr)]string lpFileName);

User-Defined Types:

None.

Notes:

Use DeleteFileW for Unicode names and DeleteFileA for ANSI names.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path or "\\?\UNC\" for fileshares on server, respectively. e.g. "\\?\D:\very long path" or "\\?\UNC\server\share"

Tips & Tricks:

Please add some!

Sample Code:

String filePath = @"C:\Data\MyFile.txt";

bool deleted = DeleteFileW(filePath);
if (!deleted)
{
     int lastError = Marshal.GetLastWin32Error();
     Console.WriteLine("Failed to delete '{1}': error={0}", lastError, filePath);
}

Alternative Managed API:

System.IO.File:

public static void Delete(string path);

Documentation
DeleteFile on MSDN