September 23, 2010

Get Drive Information in Windows Using C#

It is easy to get the drive information using C#. Information like available free space, name and volume label of the drive, total size of the drive can be easily obtained. Below is a sample code to demonstrate how the drive information can be obtained. System.IO.DriveInfo class has been used to retrieve drive information. Please note that the code is written within a try-catch block since exceptions may arise while using IO.

Code Snippet
  1. foreach (DriveInfo di in DriveInfo.GetDrives())
  2.             {
  3.                 string br = "<br/>";
  4.                 try
  5.                 {
  6.                     Response.Write("available free space: " + di.AvailableFreeSpace + br);
  7.                     Response.Write("drive format: " + di.DriveFormat + br);
  8.                     Response.Write("drive type: " + di.DriveType + br);
  9.                     Response.Write("is ready: " + di.IsReady + br);
  10.                     Response.Write("name: " + di.Name + br);
  11.                     Response.Write("root directory: " + di.RootDirectory + br);
  12.                     Response.Write("total free space: " + di.TotalFreeSpace + br);
  13.                     Response.Write("total size: " + di.TotalSize + br);
  14.                     Response.Write("volume label: " + di.VolumeLabel + br);
  15.                 }
  16.                 catch (Exception ex)
  17.                 {
  18.                     Response.Write(ex.Message);
  19.                 }
  20.             }

0 comments:

Reference: Shahed Kazi at AspNetify.com