Search code examples
c#disk

How do I retrieve disk information in C#?


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


Solution

  • 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);
            }
        }
    }