C#和diskpart:如何通过磁盘标签而不是数字进行选择? [英] C# and diskpart: how to select by disk label and not by a number?
问题描述
我有一个安装Windows映像的C#应用程序.我必须选择系统将在用户界面上复制(C:或D:或...)的磁盘.为此,没关系.
I have a C# application that install Windows image. I have to choose the disk where the system will copied (C: or D: or ...) on the user interface. For that, it's ok.
然后我必须格式化磁盘.我必须使用diskpart.exe选择与C:相关联的良好物理磁盘.但是使用diskpart时,我们选择具有编号的磁盘:选择磁盘0或1或...
Then i have to format the disk. I have to select with diskpart.exe the good physical disk associed to C:. But with diskpart, we choose the disk with number: select disk 0 or 1 or ...
如何在好磁盘号和用户在界面上选择的字母之间建立联系?
How to make the connection between the good disk number and the letter choosen by users on the interface ?
我在Google上找不到任何内容.我试图用wmi Win32_DiskDrive
查找信息,但与diskpart详细信息磁盘没有共同之处.
I found Nothing on google. I tried to find an information with wmi Win32_DiskDrive
but nothing in common with diskpart detail disk .
谢谢
推荐答案
另一个解决方案而不是使用ManagementObjectSearcher
是以编程方式使用DiskPart.exe
,但是我的代码是静态解决方案(使用regex会更好),但可以使用很久了.
Another solution instead of using ManagementObjectSearcher
is using DiskPart.exe
programatically, but my code is rather a static solution (would be better with regex) but will work a long time.
它需要具有更高执行特权的清单文件(添加新元素> Application Manifest File并将requestedExecutionLevel
更改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
.有关更多信息:https://stackoverflow.com/a/43941461/5830773 )
It requires a manifest file with higher execution privileges (Add new element > Application Manifest File and change requestedExecutionLevel
to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
. for further information: https://stackoverflow.com/a/43941461/5830773)
然后,您可以使用以下代码通过DiskPart.exe
获取驱动器列表:
Then you can use following code to get the drive list with DiskPart.exe
:
// execute DiskPart programatically
Process process = new Process();
process.StartInfo.FileName = "diskpart.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("list volume");
process.StandardInput.WriteLine("exit");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// extract information from output
string table = output.Split(new string[] { "DISKPART>" }, StringSplitOptions.None)[1];
var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
for (int i = 3; i < rows.Length; i++)
{
if (rows[i].Contains("Volume"))
{
int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];
Console.WriteLine($@"Volume {index} {label}:\");
}
}
这提供了类似DiskPart的以下输出,但是您可以根据需要对其进行自定义:
This gives following output like from DiskPart but you can customise it for your needs:
Volume 0 C:\
Volume 1 D:\
Volume 2 F:\
Volume 3 G:\
Volume 4 I:\
Volume 5 H:\
现在通过驱动器号搜索很明显:
Now searching by drive letter is obvious:
public int GetIndexOfDrive(string drive)
{
drive = drive.Replace(":", "").Replace(@"\", "");
// execute DiskPart programatically
Process process = new Process();
process.StartInfo.FileName = "diskpart.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("list volume");
process.StandardInput.WriteLine("exit");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// extract information from output
string table = output.Split(new string[] { "DISKPART>" }, StringSplitOptions.None)[1];
var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
for (int i = 3; i < rows.Length; i++)
{
if (rows[i].Contains("Volume"))
{
int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];
if (label.Equals(drive))
{
return index;
}
}
}
return -1;
}
用法:
Console.WriteLine(GetIndexOfDrive(@"D:\")); // returns 1 on my computer
这篇关于C#和diskpart:如何通过磁盘标签而不是数字进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!