Inspecting the drives on the local PC using C# .NET

It’s very simple to enumerate the drives on a Windows computer. The DriveInfo object has a GetDrives static method which returns an array of DriveInfo objects. A DriveInfo describes a drive through its properties. It is similar to the FileInfo class which in turn describes a file.

There are some properties that can only be extracted if the drive is ready, e.g. a CD-Rom drive. The following method prints the available drives:

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
	Console.WriteLine("Drive name: {0}", drive.Name);
	if (drive.IsReady)
	{
		Console.WriteLine("Total size: {0}", drive.TotalSize);
		Console.WriteLine("Total free space: {0}", drive.TotalFreeSpace);
		Console.WriteLine("Available free space: {0}", drive.AvailableFreeSpace);
		Console.WriteLine("Drive format: {0}", drive.DriveFormat);
	}
	else
	{
		Console.WriteLine("Device {0} is not ready.", drive.Name);									
	}
	Console.WriteLine("Drive type: {0}", drive.DriveType);	
}

I got the following output:

Drives enumerated

DriveType can have the following values:

  • CDRom: an optical drive such as a CD-ROM, DVD etc.
  • Fixed: a fixed disk, quite often labelled as “C:\”
  • Network: a mapped drive
  • NoRootDirectory: a drive with no root directory
  • Ram: a RAM drive
  • Removable: a removable drive such as a pen-drive
  • Unknown: a drive whose type could not be determined

Read all posts dedicated to file I/O here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: