Eine andere Lösung anstelle von ManagementObjectSearcher
ist programmatisch mit DiskPart.exe
, aber mein Code ist eher eine statische Lösung (wäre besser mit Regex), aber wird eine lange Zeit arbeiten. (. Neue Element hinzufügen> Anwendungsmanifestdatei und ändern requestedExecutionLevel
-<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
für weitere Informationen: https://stackoverflow.com/a/43941461/5830773)
Es mit höheren Ausführungsrechten eine Manifest-Datei erfordert
Dann können Sie folgenden Code verwenden, um die Laufwerksliste zu erhalten mit 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([email protected]"Volume {index} {label}:\");
}
}
Dies gibt
wie von Diskpart folgende Ausgabe, aber Sie können es für Ihre Bedürfnisse anpassen:
Volume 0 C:\
Volume 1 D:\
Volume 2 F:\
Volume 3 G:\
Volume 4 I:\
Volume 5 H:\
nun von Laufwerksbuchstaben der Suche ist offensichtlich:
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;
}
Verbrauch:
Console.WriteLine(GetIndexOfDrive(@"D:\")); // returns 1 on my computer
Sie haben vergessen, den Code hinzuzufügen. Fragen Sie, wie [Index des Elements in Array erhalten] (https://stackoverflow.com/q/4388600/1997232)? – Sinatr
Ich muss eine Diskette in WinPE mit DISKPART nach Etikett auswählen. Aber ich weiß nicht, wie ich das machen soll. Ich habe den Brief und ich würde gerne wissen, ob jemand schon versucht hat, eine Diskette mit einem Etikett auszuwählen. – simsim
Warum ist Ihre Frage mit Powershell und Cmd markiert, wenn es keine Powershell oder Cmd gibt? – Clijsters