52

システム上の論理ドライブ (C#) のリスト、およびその容量と空き容量を取得するにはどうすればよいですか?

4

7 に答える 7

66

System.IO.DriveInfo.GetDrives()

于 2009-04-23T14:13:02.467 に答える
36
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);
}
于 2009-04-23T14:16:24.377 に答える
24

Directory.GetLogicalDrives

彼らの例はより堅牢ですが、ここにその核心があります

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

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

また、 P/Invokeを実行して win32 関数を呼び出すこともできます (または、アンマネージ コードを使用している場合は使用します)。

これはドライブのリストを取得するだけですが、それぞれの情報については、Chris Ballance が示すようにGetDrivesを使用することをお勧めします。

于 2009-04-23T14:14:40.793 に答える
7

多分これはあなたが望むものです:

listBox1.Items.Clear();

foreach (DriveInfo f in DriveInfo.GetDrives())    
    listBox1.Items.Add(f);
于 2013-12-13T19:31:36.790 に答える
2

この情報は、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());
    }

ポーリングできる属性に関する詳細情報はこちら

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

于 2009-04-23T14:14:09.373 に答える