Thursday, April 11, 2013

How To- Get hard drive information using C#

In this post, I will explain how to get the hard dive information of your system using C#.

In my previous posts, I explained Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C#, Partial Methods, Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server .

TO get the hard drive information first we need to add the following namespace.

using System.IO;

Now write the following method to get the information of all the drives of your system.

/// <summary>
/// Method to get the information of all the hard drives
/// </summary>
public static void GetDriveInformation()
{
  try
  {
     //Get all the drives of system
     DriveInfo[] drives = DriveInfo.GetDrives();
     int count=0;
     foreach (DriveInfo info in drives)
     {
        if (info.IsReady)
        {
           count += 1;
           Console.WriteLine("*************Drive {0}*************", count);
           Console.WriteLine("Drive Name:{0}", info.Name);
           Console.WriteLine("Drive Size:{0} bytes", info.TotalSize.ToString());
           Console.WriteLine("Free Space:{0} bytes", info.TotalFreeSpace.ToString());
           Console.WriteLine("Drive Format:{0}", info.DriveFormat);
           Console.WriteLine("Drive Type:{0}", info.DriveType);
           Console.WriteLine("Drive Volume Label:{0}", info.VolumeLabel);
        }
     }
     Console.ReadKey();
  }
  catch (Exception ex)
  {

  }
}

I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

^ Scroll to Top