How do I retrieve disk information in C#?

前端 未结 6 1797
暖寄归人
暖寄归人 2020-11-27 18:25

I would like to access information on the logical drives on my computer using C#. How should I accomplish this? Thanks!

6条回答
  •  渐次进展
    2020-11-27 18:44

    What about mounted volumes, where you have no drive letter?

    foreach( ManagementObject volume in 
                 new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
    {
      if( volume["FreeSpace"] != null )
      {
        Console.WriteLine("{0} = {1} out of {2}",
                      volume["Name"],
                      ulong.Parse(volume["FreeSpace"].ToString()).ToString("#,##0"),
                      ulong.Parse(volume["Capacity"].ToString()).ToString("#,##0"));
      }
    }
    

提交回复
热议问题