2009-04-23 49 views

Trả lời

50
+0

Đây có phải là tính năng mới được thêm vào trong phiên bản .NET mới nhất không. Tôi đã viết một ứng dụng nhỏ để hiển thị những năm trước nhưng phải đi theo con đường WMI vào thời điểm đó. Rất tiện dụng để biết dù sao ... chúc mừng –

+0

Hoàn hảo ... cảm ơn bạn – PaulB

+0

Nhìn nhanh trên MSDN: đã được thêm vào .NET 2.0. – Richard

0

Bạn có thể lấy thông tin này với Windows Management Instrumentation (WMI) thông tin

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 thêm ở đây về các thuộc tính bạn có thể thăm dò ý kiến ​​

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

+0

Không thể làm việc này trên máy tính của tôi. System.Management hiện không có lớp ManagementObjectSearcher. URL cũng không trỏ đến trang web hợp lệ. –

+0

Bạn cần thêm tham chiếu cho điều đó. Trên Visual Studio, nhấp chuột phải vào dự án sau đó đi đến Add -> Reference. Sau đó, tìm kiếm "System.Management" và thêm nó. – Gippeumi

18

Directory.GetLogicalDrives

dụ họ có mạnh mẽ hơn, nhưng đây là mấu chốt của nó

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

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

Bạn cũng P/Invoke và có thể gọi hàm win32 (hoặc sử dụng nó nếu bạn đang ở unmanaged code).

Điều đó chỉ nhận được danh sách các ổ đĩa tuy nhiên, để biết thông tin về từng loại, bạn sẽ muốn sử dụng GetDrives như Chris Ballance trình bày.

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

có lẽ đây là những gì bạn muốn:

listBox1.Items.Clear(); 

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

Bạn cũng có thể muốn kiểm tra thuộc tính IsReady –

0

Đây là một mảnh tuyệt vời của mã.

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. 
} 
Các vấn đề liên quan