tags:

views:

1023

answers:

5

I'd like to initialize an sd card with fat16 file systems. Assuming that I have my sd reader on drive G: how I can easily format it to fat 16?

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.

+1  A: 

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
+2  A: 

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
Any safer way to convert G: to 6 than "G:"[0]-'A'?
Piotr Czapla
How can I be sure that SHFMT_ID_DEFAULT is fat16 not 32?
Piotr Czapla
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
A: 

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
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
I wasn't able to run the Format method as described so we finally use the format.exe with parameters.
Piotr Czapla
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
+1  A: 

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
A: 

BUT "FORMAT" DOESNOT work In XP.. please explain

amita