2016-12-23 1 views
1

Wie bekomme ich eine Liste von Festplatten und ihre Partitionen (ihre logischen Laufwerke) auf meinem Computer in C#? iam für Code suchen, der mir ähnliche ErgebnisseListe der Festplatten und Laufwerke auf ihnen

harddisk0 gibt: Partitionen C, D

harddisk1: Partitionen C, F, D

ich versucht habe diesen Code

foreach (ManagementObject drive in search.Get()) 
{ 
    string antecedent = drive["DeviceID"].ToString(); 
    // the disk we're trying to find out about 
    antecedent = antecedent.Replace(@"\", "\\"); 
    // this is just to escape the slashes 
    string query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" 
        + antecedent 
        + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"; 
    using (ManagementObjectSearcher partitionSearch = new ManagementObjectSearcher(query)) 
     { 
     foreach (ManagementObject part in partitionSearch.Get()) 
     { 
      //...pull out the partition information 
      Console.WriteLine("Dependent : {0}", part["Dependent"]); 
      } 
     } 
} 

zu wissen, dass abhängig ein Verweis auf die Instanz ist, die die Festplattenpartition darstellt, die sich auf dem Festplattenlaufwerk befindet. aber ich bekomme die Ausnahme nicht gefunden

was soll ich bitte schreiben?

+0

Mögliches Duplikat [Liste aller Partitionen auf Disk] beschrieben wird (http://stackoverflow.com/questions/6575727/list-all-partitions-on-disk) – Sorceri

+0

nein mein Problem wurde nicht von diesem Link gelöst http://stackoverflow.com/questions/6575727/list-all-partitions-on-disk –

+0

war es gelöst, indem Sie den Plan von diesem Skript https://blogs.technet.microsoft.com/heyscriptingguy/2005/05/23/how-can-i-corre erhalten späte logische Laufwerke und physische Laufwerke –

Antwort

1

hier ist C# Lösung von mir erzeugt

foreach (ManagementObject drive in search.Get()) 
      { 

       string antecedent = drive["DeviceID"].ToString(); // the disk we're trying to find out about 
       antecedent = antecedent.Replace(@"\", "\\"); // this is just to escape the slashes 
       string query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + antecedent + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"; 
       using (ManagementObjectSearcher partitionSearch = new ManagementObjectSearcher(query)) 
       { 
        foreach (ManagementObject part in partitionSearch.Get()) 
        { 
         //...pull out the partition information 
         MessageBox.Show(part["DeviceID"].ToString()); 
         query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + part["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"; 
         using (ManagementObjectSearcher logicalpartitionsearch = new ManagementObjectSearcher(query)) 
          foreach (ManagementObject logicalpartition in logicalpartitionsearch.Get()) 
          MessageBox.Show(logicalpartition["DeviceID"].ToString()); 
        } 

       } 
      } 

der Plan dieses Codes in diesem Skript https://blogs.technet.microsoft.com/heyscriptingguy/2005/05/23/how-can-i-correlate-logical-drives-and-physical-disks/

Verwandte Themen