How do I retrieve disk information in C#?

前端 未结 6 1798
暖寄归人
暖寄归人 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 19:07

    For most information, you can use the DriveInfo class.

    using System;
    using System.IO;
    
    class Info {
        public static void Main() {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives) {
                //There are more attributes you can use.
                //Check the MSDN link for a complete example.
                Console.WriteLine(drive.Name);
                if (drive.IsReady) Console.WriteLine(drive.TotalSize);
            }
        }
    }
    

提交回复
热议问题