Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Getting Drive's Volume Information using C#

0.00/5 (No votes)
20 Dec 2007 2  
Getting Drive's Volume Information using C#

Introduction

First of all, I want to say sorry for my poor English. In this article, I try to explain how to get the volume information of our logical drives by using kernel32.dll.

Kernel32.dll

This DLL supports capabilities that are associated with OS such as:

  • Process loading
  • Context switching
  • File I/O
  • Memory management

GetVolumeInformation

This function is used to get the volume information of drive. It gives serial number, volume name and file type.

The following is the declaration of GetVolumeInformation function:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool GetVolumeInformation(string Volume, StringBuilder VolumeName, 
	uint VolumeNameSize, out uint SerialNumber, out uint SerialNumberLength, 
	out uint flags, StringBuilder fs, uint fs_size);

DllImport is used to import the kernel32.dll file. The out variable SerialNumber will return the serial number of a given drive. Here, the following code will explain how to get volume information of a given drive.

uint serialNum, serialNumLength, flags;
StringBuilder volumename = new StringBuilder(256);
StringBuilder fstype = new StringBuilder(256); 

bool ok = = GetVolumeInformation(drives, volumename, 
	(uint)volumename.Capacity - 1, out serialNum, out serialNumLength, 
	out flags, fstype, (uint)fstype.Capacity - 1);

if (ok)
 {
  lblVolume.Text = lblVolume.Text + "\n Volume Information of " + drives + "\n";
  lblVolume.Text = lblVolume.Text + "\nSerialNumber of is..... " + 
	serialNum.ToString() + " \n";
   if (volumename != null)
   {
   lblVolume.Text = lblVolume.Text + "VolumeName is..... " + 
	volumename.ToString() + " \n";
   }
if (fstype != null)
 {
 lblVolume.Text = lblVolume.Text + "FileType is..... " + fstype.ToString() + " \n";
 }

Hope this will help you. :)

History

  • 20th December, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here