簡體   English   中英

C# 和 diskpart:如何通過磁盤標簽而不是數字進行選擇?

[英]C# and diskpart: how to select by disk label and not by a number?

我有一個安裝 Windows 映像的 C# 應用程序。 我必須在用戶界面上選擇系統將復制到的磁盤(C: 或 D: 或 ...)。 為此,沒關系。

然后我必須格式化磁盤。 我必須使用diskpart.exe 選擇與C: 關聯的良好物理磁盤。 但是對於diskpart,我們選擇帶有編號的磁盤:選擇磁盤0或1或...

如何將好的盤號與用戶在界面上選擇的盤符進行連接?

我在谷歌上什么也沒找到。 我嘗試使用 wmi Win32_DiskDrive查找信息,但與 diskpart detail disk 沒有任何共同之處。

謝謝

另一種不使用ManagementObjectSearcher解決方案是以編程方式使用DiskPart.exe ,但我的代碼是一個靜態解決方案(使用正則表達式會更好)但會工作很長時間。

它需要具有較高的執行權限(添加新元素的清單文件>應用程序清單文件和變化requestedExecutionLevel<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />進一步的信息: https://stackoverflow.com/a/ 43941461/5830773 )

然后您可以使用以下代碼通過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 的以下輸出,但您可以根據需要對其進行自定義:

Volume 0 C:\
Volume 1 D:\
Volume 2 F:\
Volume 3 G:\
Volume 4 I:\
Volume 5 H:\

現在按驅動器號搜索很明顯:

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:、D: 等)之間進行映射,您必須從以下位置獲取信息:

Win32_LogicalDisk
Win32_LogicalDiskToPartition
Win32_DiskPartition

以任何順序。 這些表將包含所有信息。 在 c# 中使用System.Management可以輕松使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM