List the Drives on your Computer using C# or VB.NET

If you want to create a program to provide information about your Computer Drive, then use the System.IO.DriveInfo class. The DriveInfo class can be used to determine the drives available on your computer, drive capacity, free space, type of drives etc. as shown below:

C#


// Add namespace System.IO;


DriveInfo[] myDrives = DriveInfo.GetDrives();


foreach (DriveInfo di in myDrives)


{                


    Console.WriteLine(di.Name);


    if (di.IsReady)


    {


        Console.WriteLine(di.TotalSize);


        Console.WriteLine(di.DriveFormat);


        Console.WriteLine(di.AvailableFreeSpace);


        Console.WriteLine(di.TotalFreeSpace);


        Console.WriteLine(di.DriveType);


        Console.WriteLine(di.VolumeLabel);


    }


}




VB.NET


    ' Add namespace System.IO;


    Dim myDrives() As DriveInfo = DriveInfo.GetDrives()


    For Each di As DriveInfo In myDrives


        Console.WriteLine(di.Name)


        If di.IsReady Then


            Console.WriteLine(di.TotalSize)


            Console.WriteLine(di.DriveFormat)


            Console.WriteLine(di.AvailableFreeSpace)


            Console.WriteLine(di.TotalFreeSpace)


            Console.WriteLine(di.DriveType)


            Console.WriteLine(di.VolumeLabel)


        End If


    Next di




Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: