469
In this example we will list all disk drives in C#
Example
We use the DriveInfo.GetDrives function to list all the drives and then we loop through them all to show the name and type of the drive, for every drive we also display the total size of the drive
using System; using System.IO; namespace ConsoleApp1 { class Program { static void Main(string[] args) { DriveInfo[] drivesList = DriveInfo.GetDrives(); foreach (DriveInfo myDrives in driveList) { Console.WriteLine("Drive {0}", myDrives.Name); Console.WriteLine(" File type: {0}", myDrives.DriveType); if (myDrives.IsReady == true) { Console.WriteLine(" Total size of drive:{0, 15} bytes ", myDrives.TotalSize); Console.Read(); } } } } }
When you run this you will see something like this, this was a few of the drives on my system
Drive C:\ File type: Fixed Total size of drive: 422346682368 bytes Drive D:\ File type: Fixed Total size of drive: 400085811200 bytes Drive E:\ File type: Fixed Total size of drive: 300058181632 bytes Drive F:\ File type: Fixed Total size of drive: 300049952768 bytes Drive G:\ File type: Fixed Total size of drive: 8001427599360 bytes