格式化USB驱动器编程
本文关键字:编程 驱动器 USB 格式化 | 更新日期: 2023-09-27 18:17:32
我正在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;
}
我不太确定这是如何工作的。这是正确的方式来接近格式化USB驱动器?如果没有,有人能给我指个方向吗?
我试过看Win32_Volume
类,但是,再一次,我真的不明白它是如何工作的。这个问题建议使用CreateFile
函数。我也看了这个网站。
如果你能给我指点一下方向,我将不胜感激。
也许我还有另一个方法:
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);
我也尝试过这个,并工作:
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
也许会有帮助。