2009-04-23 18 views

risposta

50
+0

È qualcosa di nuovo che è stato aggiunto nell'ultima versione di .NET. Ho scritto una piccola app da mostrare anni fa, ma al momento dovevo seguire la rotta WMI. Molto utile sapere in ogni caso ... evviva –

+0

Perfetto ... grazie – PaulB

+0

Sguardo rapido su MSDN: è stato aggiunto in .NET 2.0. – Richard

0

È possibile recuperare queste informazioni con Windows Management Instrumentation (WMI)

using System.Management; 

    ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); 
    // Loop through each object (disk) retrieved by WMI 
    foreach (ManagementObject moDisk in mosDisks.Get()) 
    { 
     // Add the HDD to the list (use the Model field as the item's caption) 
     Console.WriteLine(moDisk["Model"].ToString()); 
    } 

Theres maggiori info qui circa l'attributo è possibile interrogare

http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

+0

Impossibile farlo funzionare sul mio computer. System.Management non ha ora la classe ManagementObjectSearcher. Anche l'URL non punta a una pagina Web valida. –

+0

È necessario aggiungere un riferimento per questo. Su Visual Studio, fare clic con il tasto destro del mouse sul progetto, quindi selezionare Aggiungi -> Riferimento. Quindi, cerca "System.Management" e aggiungilo. – Gippeumi

18

Directory.GetLogicalDrives

Il loro esempio ha più robusto, ma ecco il punto cruciale di esso

  string[] drives = System.IO.Directory.GetLogicalDrives(); 

      foreach (string str in drives) 
      { 
       System.Console.WriteLine(str); 
      } 

Si potrebbe anche P/Invoke e chiamare la funzione Win32 (o usarlo se siete in codice non gestito).

Questo ottiene solo un elenco delle unità tuttavia, per informazioni su ciascuna di esse, si desidera utilizzare GetDrives come dimostra Chris Ballance.

24
foreach (var drive in DriveInfo.GetDrives()) 
{ 
    double freeSpace = drive.TotalFreeSpace; 
    double totalSpace = drive.TotalSize; 
    double percentFree = (freeSpace/totalSpace) * 100; 
    float num = (float)percentFree; 

    Console.WriteLine("Drive:{0} With {1} % free", drive.Name, num); 
    Console.WriteLine("Space Remaining:{0}", drive.AvailableFreeSpace); 
    Console.WriteLine("Percent Free Space:{0}", percentFree); 
    Console.WriteLine("Space used:{0}", drive.TotalSize); 
    Console.WriteLine("Type: {0}", drive.DriveType); 
} 
3

forse questo è ciò che si vuole:

listBox1.Items.Clear(); 

foreach (DriveInfo f in DriveInfo.GetDrives())  
    listBox1.Items.Add(f); 
+0

Si potrebbe anche voler controllare la proprietà IsReady –

0

Questo è un meraviglioso pezzo di codice.

ObjectQuery query = 
    new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"); // Create query to select all the hdd's 

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher(scope, query); // run the query 

ManagementObjectCollection queryCollection = searcher.Get(); // get the results 
string sVolumeLabel = ""; 
string[,] saReturn = new string[queryCollection.Count, 7]; 
int i = 0; // counter for foreach 

foreach (ManagementObject m in queryCollection) 
{ 
    if (string.IsNullOrEmpty(Convert.ToString(m["VolumeName"]))) { sVolumeLabel = "Local Disk"; } else { sVolumeLabel = Convert.ToString(m["VolumeName"]); } // Disk Label 
    string sSystemName = Convert.ToString(m["SystemName"]); // Name of computer 
    string sDriveLetter = Convert.ToString(m["Name"]); // Drive Letter 

    decimal dSize = Math.Round((Convert.ToDecimal(m["Size"])/1073741824), 2); //HDD Size in Gb 
    decimal dFree = Math.Round((Convert.ToDecimal(m["FreeSpace"])/1073741824), 2); // Free Space in Gb 
    decimal dUsed = dSize - dFree; // Used HDD Space in Gb 

    int iPercent = Convert.ToInt32((dFree/dSize) * 100); // Percentage of free space 

    saReturn[i,0] = sSystemName; 
    saReturn[i,1] = sDriveLetter; 
    saReturn[i,2] = sVolumeLabel; 
    saReturn[i,3] = Convert.ToString(dSize); 
    saReturn[i,4] = Convert.ToString(dUsed); 
    saReturn[i,5] = Convert.ToString(dFree); 
    saReturn[i,6] = Convert.ToString(iPercent); 

    i++; // increase counter. This will add the above details for the next drive. 
} 
Problemi correlati