我想使用FAT16文件系统初始化SD卡。假设我的SD读卡器在G驱动器上,我该如何轻松将其格式化为FAT16?
更新: 为了澄清,我想在.net平台上使用C#进行操作,以便我可以检测错误并且可以在Windows XP及以上版本上运行。
我想使用FAT16文件系统初始化SD卡。假设我的SD读卡器在G驱动器上,我该如何轻松将其格式化为FAT16?
更新: 为了澄清,我想在.net平台上使用C#进行操作,以便我可以检测错误并且可以在Windows XP及以上版本上运行。
我尝试了上面的答案,但很遗憾它并不像看起来那么简单...
第一个答案,使用管理对象看起来是正确的方法,但不幸的是,在Windows XP中不支持“Format”方法。
第二个和第三个答案都可以工作,但需要用户确认操作。
为了在没有任何用户干预的情况下完成该操作,我使用了第二个选项,并重定向了进程的输入和输出流。当我只重定向输入流时,进程失败了。
以下是一个示例:
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();
}
}
[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" );
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "format";
processStartInfo.Arguments ="/FS:FAT G:";
Process.Start(processStartInfo);
然而,这种方法相当脆弱,如果没有解析输出,您可能无法确定是否成功。总的来说,我会谨慎考虑,并问问自己是否真的想允许应用程序内部的格式。
在DriveInfo等中找不到函数,但您始终可以使用(创建)包含Format G: /FS:FAT
的批处理文件,并使用System.Diagnostics.Process启动它。
如果你只是想快速格式化现有的格式类型,不需要指定任何内容。让系统使用默认值即可。
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");
在命令提示符下,它是这样的:
format.com H: /V:MyVolumeName /Q