我正在用C#开发一个应用程序,这样,如果用户确认消息框要格式化从组合框列表中选择的USB驱动器,驱动器将被格式化。
然而,我不知道如何处理这个问题--我有以下代码:
public static bool FormatDrive(string driveLetter,
string fileSystem = "FAT", bool quickFormat = false,
int clusterSize = 4096, string label = "", bool enableCompression = false)
{
if (driveLetter.Length != 2 || driveLetter[1] != ':' || !char.IsLetter(driveLetter[0]))
return false;
//query and format given drive
ManagementObjectSearcher searcher = new ManagementObjectSearcher
(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
foreach (ManagementObject vi in searcher.Get())
{
vi.InvokeMethod("Format", new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });
}
return true;
} 我不太确定this是如何工作的。这是格式化USB驱动器的正确方法吗?如果没有,有人能给我指个方向吗?
我试着研究过Win32_Volume类,但我还是不太理解它是如何工作的。This问题建议使用CreateFile函数。我也看过this的网站。
任何将我推向正确方向的技巧,都将不胜感激。
发布于 2015-10-14 16:54:17
好吧,也许我有另一种方法:
public static bool FormatDrive_CommandLine(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
{
#region args check
if (!Char.IsLetter(driveLetter) ||
!IsFileSystemValid(fileSystem))
{
return false;
}
#endregion
bool success = false;
string drive = driveLetter + ":";
try
{
var di = new DriveInfo(drive);
var psi = new ProcessStartInfo();
psi.FileName = "format.com";
psi.CreateNoWindow = true; //if you want to hide the window
psi.WorkingDirectory = Environment.SystemDirectory;
psi.Arguments = "/FS:" + fileSystem +
" /Y" +
" /V:" + label +
(quickFormat ? " /Q" : "") +
((fileSystem == "NTFS" && enableCompression) ? " /C" : "") +
(clusterSize.HasValue ? " /A:" + clusterSize.Value : "") +
" " + drive;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
var formatProcess = Process.Start(psi);
var swStandardInput = formatProcess.StandardInput;
swStandardInput.WriteLine();
formatProcess.WaitForExit();
success = true;
}
catch (Exception) { }
return success;
}首先我自己写了代码,现在在http://www.metasharp.net/index.php/Format_a_Hard_Drive_in_Csharp上找到了一个完美的方法
评论中的问题答案:
如果不想让/q快速格式化,请将其移除
如果需要,/x参数会强制卸载所选的卷。
来源:http://ccm.net/faq/9524-windows-how-to-format-a-usb-key-from-the-command-prompt
psi.CreateNoWindow = true;隐藏终端,使您的应用程序看起来很流畅。我的建议是在调试时显示它。
您要调用的是驱动器为F:/的情况,例如:
FormatDrive_CommandLine('F', "formattedDrive", "FAT32", false, false);发布于 2017-10-31 20:25:54
我也尝试过了,并取得了成效:
public bool FormatUSB(string driveLetter, string fileSystem = "FAT32", bool quickFormat = true,
int clusterSize = 4096, string label = "USB_0000", bool enableCompression = false)
{
//add logic to format Usb drive
//verify conditions for the letter format: driveLetter[0] must be letter. driveLetter[1] must be ":" and all the characters mustn't be more than 2
if (driveLetter.Length != 2 || driveLetter[1] != ':' || !char.IsLetter(driveLetter[0]))
return false;
//query and format given drive
//best option is to use ManagementObjectSearcher
var files = Directory.GetFiles(driveLetter);
var directories = Directory.GetDirectories(driveLetter);
foreach (var item in files)
{
try
{
File.Delete(item);
}
catch (UnauthorizedAccessException) { }
catch (IOException) { }
}
foreach (var item in directories)
{
try
{
Directory.Delete(item);
}
catch (UnauthorizedAccessException) { }
catch (IOException) { }
}
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
foreach (ManagementObject vi in searcher.Get())
{
try
{
var completed = false;
var watcher = new ManagementOperationObserver();
watcher.Completed += (sender, args) =>
{
Console.WriteLine("USB format completed " + args.Status);
completed = true;
};
watcher.Progress += (sender, args) =>
{
Console.WriteLine("USB format in progress " + args.Current);
};
vi.InvokeMethod(watcher, "Format", new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });
while (!completed) { System.Threading.Thread.Sleep(1000); }
}
catch
{
}
}
return true;
}
#endregion也许这会有所帮助。
https://stackoverflow.com/questions/33120387
复制相似问题