articles

Home / DeveloperSection / Articles / Get list of logical drives information using C#

Get list of logical drives information using C#

Vijay Shukla9350 10-Oct-2013

Get list of logical drives information using C#

In this article I am making a small demo application for getting the logical drives information using C#. Here I create DriveInfoinstance to get list of capacity of drive, used size of drive, free drive size and file type of logical drives.

Getting Start: -

1.       Create a console project in visual studio with appropriate name.

2.       In the Main() write the below line of code for getting the logical drives information.

  

DriveInfo[] drives = DriveInfo.GetDrives();// Create DriveInfo Type variable and get the all information of drives with the help of DriveInfo.GetDrives() method.

Console.WriteLine("=================================");

Console.WriteLine("\tProperties");

Console.WriteLine("=================================");

foreach (DriveInfo drive in drives)

{

  Console.WriteLine("=================================");

  Console.WriteLine("\tProperties");

  Console.WriteLine("=================================");

  Console.WriteLine("Drive: {0}", drive.Name); // Show drive name

  if (drive.TotalSize > 1073741824) //check the drive is MB or GB

      Console.WriteLine("Used Space: {0}GB", (drive.TotalSize / 1073741824) - (drive.AvailableFreeSpace / 1073741824)); // Shows Used drive space in GB if total size in GB

    else

      Console.WriteLine("Used Space: {0}MB", (drive.TotalSize / 1073741824) - (drive.AvailableFreeSpace / 1073741824)); // Shows Used drive space in MB if total size in MB   

if (drive.AvailableFreeSpace > 1073741824)

       Console.WriteLine("Space Reamining:{0}GB", drive.AvailableFreeSpace / 1073741824); // Shows available drive space in MB if total size in GB   

    else

       Console.WriteLine("Space Reamining:{0}MB", drive.AvailableFreeSpace / 1073741824); // Shows available drive space in MB if total size in MB   

    if (drive.TotalSize > 1073741824)

       Console.WriteLine("Capacity:{0}GB", drive.TotalSize / 1073741824); // Shows Total drive space in MB if total size in GB   

    else

       Console.WriteLine("Capacity:{0}MB", drive.TotalSize / 1073741824); // Shows Total drive space in MB if total size in MB   

 

       Console.WriteLine("File System: {0}", drive.DriveFormat); // Shows Total drive File System   

         

}

Console.ReadKey();

Note :- copy the above line of code and paste into your main folder and run your demo project.

Output: -

Get list of logical drives information using C#


Updated 07-Sep-2019

Leave Comment

Comments

Liked By