Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c# - How to programatically format an SD card with FAT16?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...