3

Windows イメージをインストールする C# アプリケーションがあります。ユーザー インターフェイスで、システムがコピーするディスク (C: または D: または ...) を選択する必要があります。そのために、それは大丈夫です。

次に、ディスクをフォーマットする必要があります。C: に関連付けられている適切な物理ディスクを diskpart.exe で選択する必要があります。しかし、diskpart では、番号付きのディスクを選択します: select disk 0 or 1 or ...

インターフェイスでユーザーが選択したディスク番号と文字を関連付けるにはどうすればよいですか?

私はグーグルで何も見つかりませんでした。wmi で情報を見つけようとしましWin32_DiskDriveたが、 diskpart detail disk との共通点はありません。

ありがとう

4

2 に答える 2

3

使用する代わりの別の解決策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
于 2017-10-26T14:16:51.280 に答える