3

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: To clarify, I'd like to do that on .net platform using C# in a way that I can detect errors and that would work on Windows XP and above.

Lennart
  • 9,657
  • 16
  • 68
  • 84
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
  • Considering that the question is tagged c# and .net, I suppose this is to be done programmatically. Hence this question does not belong on superuser (there has been a close vote for this). – balpha Aug 05 '09 at 10:46
  • 2
    How do you mean "belong on superuser"? – Piotr Czapla Aug 05 '09 at 11:45
  • balpha said _not_ on superuser. See bottom of this page for SU – H H Aug 05 '09 at 12:13

6 Answers6

3

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" );
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Any safer way to convert G: to 6 than "G:"[0]-'A'? – Piotr Czapla Aug 05 '09 at 11:37
  • How can I be sure that SHFMT_ID_DEFAULT is fat16 not 32? – Piotr Czapla Aug 05 '09 at 11:39
  • Have you notice this note on MSDN? This function is available through Windows XP Service Pack 2 (SP2) and Windows Server 2003. It might be altered or unavailable in subsequent versions of Windows – Piotr Czapla Aug 05 '09 at 11:40
  • This totally works, but should be noted that this will bring up the Format dialog (at least it did for me on WinXP). In my scenario, this is not a helpful User Interaction experience, so I'm back to the drawing board. – oddmeter May 01 '13 at 16:12
3

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();

    }
}
Barak C
  • 169
  • 4
1

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

H H
  • 263,252
  • 30
  • 330
  • 514
1

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.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
1

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.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Could you copy the WMI method to your answer from the article so that I can accept your answer? I think it is better to have the answer in stackoverflow in case the original post is deleted. – Piotr Czapla Aug 05 '09 at 11:53
  • I wasn't able to run the Format method as described so we finally use the format.exe with parameters. – Piotr Czapla Oct 01 '09 at 18:06
  • The following note from [MSDN Library](http://msdn.microsoft.com/en-us/library/aa394515%28v=VS.85%29.aspx) should be noted: "Windows XP and earlier: This class is not available." – lordhog Sep 24 '10 at 03:29
  • Note: ManagementObject is in System.Management, which isn't referenced by default. – dlchambers Apr 04 '18 at 14:14
0

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
Chizl
  • 2,004
  • 17
  • 32