SHFormatDrive (shell32)
Last changed: -80.11.84.239

.
Summary

C# Signature:

[DllImport("shell32.dll")]
static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID,
   uint options);

User-Defined Types:

public enum SHFormatFlags : uint {
     SHFMT_ID_DEFAULT = 0xFFFF,
     SHFMT_OPT_FULL = 0x1,
     SHFMT_OPT_SYSONLY = 0x2,
     SHFMT_ERROR = 0xFFFFFFFF,
     SHFMT_CANCEL = 0xFFFFFFFE,
     SHFMT_NOFORMAT = 0xFFFFFFD,
}

Another C# Signature:

[DllImport("shell32.dll")]
static extern uint SHFormatDrive(IntPtr hwnd, uint drive, SHFormatFlags fmtID,
                       SHFormatOptions options);

User-Defined Types:

public enum SHFormatFlags : uint
{
     SHFMT_ID_DEFAULT = 0xFFFF,
}

[Flags]
public enum SHFormatOptions : uint
{
     SHFMT_OPT_FULL = 0x1,
     SHFMT_OPT_SYSONLY = 0x2,
}

User-Defined Helper Method:

public const uint SHFMT_ERROR       = 0xFFFFFFFF;
public const uint SHFMT_CANCEL      = 0xFFFFFFFE;
public const uint SHFMT_NOFORMAT    = 0xFFFFFFD;

/// <summary>
/// Permit to check the result of the format call
/// Throw Exception with a detailed message
/// </summary>
/// <param name="shFormatResult"></param>
public static void CheckFormatResult(uint shFormatResult)
{
     if (shFormatResult == SHFMT_ERROR)
     throw new Exception("An error occurred during the format. This does not indicate that the drive is unformattable.");
     if (shFormatResult == SHFMT_CANCEL)
     throw new OperationCanceledException("The format was canceled.");
     if (shFormatResult == SHFMT_NOFORMAT)
     throw new IOException("The drive cannot be formatted.");

     //we can exit normally
     return;
}

Notes:

The hwnd field should be the handle of the required parent calling window (see http://msdn.microsoft.com/en-us/library/bb762169.aspx). passing in <parent window>.Handle (in C#) works in XP, but in Vista it does not. What works on Vista is passing in 0 (IntPtr.Zero). I have tested this on WP SP2 and it works there as well.

The fmtID field should always be passed SHFMT_ID_DEFAULT.

The drive field should denote the numerical listing of the drive letter to be formatted. (Ex: A is 0, Z is 25)

The options field should be 0 for a full disk format, pass SHFMT_OPT_FULL for a Quick Format, or set SHFMT_OPT_SYSONLY to create an MS-DOS System Boot Disk.

The function will return the ID of the last successful format, or one of the following error codes:

- SHFMT_ERROR: A general error occured while formatting. This is not an indication that the drive cannot be formatted though.

- SHFMT_CANCEL: The drive format was cancelled by user/OS.

- SHFMT_NOFORMAT: A serious error occured while formatting. The drive is unable to be formatted by the OS.

Tips & Tricks:

Converting a DriveInfo to drive number in C#:

DriveInfo drive = new DriveInfo("F:\\");
byte[] bytes = Encoding.ASCII.GetBytes(drive.Name.ToCharArray());
uint driveNumber = Convert.ToUInt32(bytes[0] - Encoding.ASCII.GetBytes(new [] {'A'})[0]);

Sample Code:

C# Sample Call:

uint result = SHFormatDrive( this.Handle,
              2, // formatting C:
              (uint)SHFormatFlags.SHFMT_ID_DEFAULT,
              0 ); // full format of C:
if ( result == SHFormatFlags.SHFMT_ERROR )
    MessageBox.Show( "Unable to format the drive" );

NOTE- See the Note above about the hwnd field and Vista. Use

          SHFormatDrive( IntPtr.Zero,
              2, // formatting C:
              (uint)SHFormatFlags.SHFMT_ID_DEFAULT,
              0 ); // full format of C:

On Vista and better, you also have the Win32_Volume WMI classes. See http://msdn.microsoft.com/en-us/library/aa390432(VS.85).aspx

Another Sample Code:

byte[] bytes = Encoding.ASCII.GetBytes(drive.Name.ToCharArray());
uint driveNumber = Convert.ToUInt32(bytes[0] - Encoding.ASCII.GetBytes(new [] {'A'})[0]);

uint shFormatResult = SHFormatDrive(IntPtr.Zero, driveNumber, SHFormatFlags.SHFMT_ID_DEFAULT, SHFormatOptions.SHFMT_OPT_FULL);
CheckFormatResult(shFormatResult);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation