windows 如何使用 FAT16 以编程方式格式化 SD 卡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1232398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programatically format an SD card with FAT16?
提问by Piotr Czapla
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 ?
我想用 FAT16 文件系统初始化 SD 卡。假设我的 SD 读卡器在驱动器 G: 上,我如何轻松地将其格式化为 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.
更新:澄清一下,我想在使用 C# 的 .net 平台上以一种可以检测错误的方式执行此操作,并且可以在 Windows XP 及更高版本上运行。
采纳答案by Barak C
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.
第一个答案,使用管理对象看起来是这样做的正确方法,但不幸的是,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();
}
}
回答by Preet Sangha
You could use pinvoke to call SHFormatDrive.
您可以使用pinvoke 调用 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" );
回答by Preet Sangha
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:
WMI 方法似乎没有 C# 示例,但我四处寻找并构造了这个:
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 herefor more info.
请参阅此处了解更多信息。
回答by Henk Holterman
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
在 DriveInfo 等中找不到函数,但您始终可以使用(创建)一个批处理文件,其中包含Format G: /FS:FAT
System.Diagnostics.Process 并启动它
回答by Rob Levine
Assuming you are actually asking how to do this in C# (from the tag you've applied to the question):
假设您实际上是在询问如何在 C# 中执行此操作(来自您应用于问题的标签):
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.
但是,这是一种非常脆弱的方法,如果不解析输出,您可能无法判断这是否成功。总体而言,我会保持谨慎,并问问自己是否真的希望允许在应用程序中使用格式。