I\'d like to initialize an SD card with FAT16 file system. Assuming that I have my SD reader on drive G:, how I can easily format it to FAT16 ?
UPDATE:
Assuming you are actually asking how to do this in C# (from the tag you've applied to the question):
I don't believe there is a framework way of formatting a drive, so you may have to fall back to something along the lines of
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "format";
processStartInfo.Arguments ="/FS:FAT G:";
Process.Start(processStartInfo);
However, this is a pretty brittle way of doing this, and without parsing the output you may not be able to tell if this was successfull. I'd be cautious overall and ask yourself if you really want to allow a format from within your application.
You could use pinvoke to call SHFormatDrive.
[DllImport("shell32.dll")]
static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);
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,
}
//(Drive letter : A is 0, Z is 25)
uint result = SHFormatDrive( this.Handle,
6, // formatting C:
(uint)SHFormatFlags.SHFMT_ID_DEFAULT,
0 ); // full format of g:
if ( result == SHFormatFlags.SHFMT_ERROR )
MessageBox.Show( "Unable to format the drive" );
Couldn't find a function in DriveInfo et al, but you can always use (create) a batch file containing Format G: /FS:FAT
and start it with System.Diagnostics.Process
If you just want a quick format of the existing format type, no need to specify anything. Let the system use defaults.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "format.com";
startInfo.Arguments = $"{drive} /V:{volumeName} /Q"
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
Process.Start(startInfo);
//because there will be a prompt, this input by passes that prompt.
StreamWriter processInputStream = p.StandardInput;
processInputStream.Write("\r\n");
At the command prompt it's like this:
format.com H: /V:MyVolumeName /Q
I tried the answers above, unfortunately it was not simple as it seems...
The first answer, using the management object looks like the correct way of doing so but unfortunately the "Format" method is not supported in windows xp.
The second and the third answers are working but require the user to confirm the operation.
In order to do that without any intervention from the user I used the second option with redirecting the input and output streams of the process. When I redirecting only the input stream the process failed.
The following is an example:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && (d.DriveType == DriveType.Removable))
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "format";
startInfo.Arguments = "/fs:FAT /v:MyVolume /q " + d.Name.Remove(2);
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
Process p = Process.Start(startInfo);
StreamWriter processInputStream = p.StandardInput;
processInputStream.Write("\r\n");
p.WaitForExit();
}
}
There is a host of answers here
The WMI method doesn't seem to have a C# example but I had a hunt around and constructed this:
ManagementObject disk = new ManagementObject("SELECT * FROM Win32_Volume WHERE Name = 'G:\\\\'");
disk.Get();
disk.InvokeMethod("Format", new object[] {"FAT", false, 4096, "TheLabel", false});
I don't have a drive spare to test this on, so the cluster size could be wrong.
See here for more info.