Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

Formatting a Drive using C# and WMI

Rate me:
Please Sign up or sign in to vote.
4.80/5 (8 votes)
5 Oct 2010CPOL2 min read 65.5K   2.4K   21   6
Formatting a drive using C# and WMI

Introduction

Formatting a Drive under Windows is pretty easy using the Explorer context menu or the good old FORMAT in a console window. And although the .NET Framework contains a huge set of useful file manipulating classes and methods, formatting a drive cannot be found in it. Unfortunately, for an automated CF-Card copy station, I needed a way to reinitialize the flash drives by code.

So I went down the technological letter to find a suiting API to do so and as WMI is always a good point to look for hardware stuff, I first began to find something about it in the painful huge clowd of WMI classes and found the answer surprisingly fast.

The Win32_Volume class, which can be easily queried using the drive letter, holds a Format method to do the job.

Using a WMI class in .NET via the System.Management namespace is also an easy task if you know how to do it right. To save a lot of time on searching all things together for all others out there having the same problem, I decided to write this small article.

Using the Code

I did not build a huge class construct, but a single method in the provided sample application to show the trick. I recommend simply to copy the method code into your project, but don't forget to add the System.Management reference into your project.

The whole bunch of work is done by the FormatVolume method:

C#
public static bool FormatDrive(string driveLetter, 
	string fileSystem = "NTFS", bool quickFormat=true, 
	int clusterSize = 8192, string label = "", bool enableCompression = false )
{
   if (driveLetter.Length != 2 || driveLetter[1] != ':'|| !char.IsLetter(driveLetter[0]))
      return false;

   //query and format given drive         
   ManagementObjectSearcher searcher = new ManagementObjectSearcher
	(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
   foreach (ManagementObject vi in searcher.Get())
   {
      vi.InvokeMethod("Format", new object[] 
	{ fileSystem, quickFormat,clusterSize, label, enableCompression });
   }

   return true;
} 

It uses named parameter, which is the C# 4.0 syntax to prevent all the necessary overloads. For earlier Framework versions, you need to fix this!

I know, it's not programmed very fail-proof, feel free to enhance this in your project

Points of Interest

The Win32_Volume class is not available under Windows XP and earlier, but I'm sure there is any another class which also provides that functionality, maybe I'll find it and then I'll update the article.

History

  • 5th October, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoesn't work with unformatted USB drives Pin
Carl Compac10-Sep-14 17:39
Carl Compac10-Sep-14 17:39 
Generalusing IDisposable instances [modified] Pin
freihaupt25-Feb-11 0:02
freihaupt25-Feb-11 0:02 
GeneralRe: using IDisposable instances Pin
pappe8225-Feb-11 2:00
pappe8225-Feb-11 2:00 
GeneralRe: using IDisposable instances [modified] Pin
Pieter Jan van der Stoel28-Jul-14 4:05
Pieter Jan van der Stoel28-Jul-14 4:05 
GeneralCan't execute this sample (Windows XP) Pin
Temp37818-Jan-11 8:53
Temp37818-Jan-11 8:53 
Can't execute this sample (Windows XP).
"An unhandled exception of type 'System.Management.ManagementException' occurred in System.Management.dll" on line 44:
foreach (ManagementObject vi in searcher.Get())


Maybe you know how to solve this problem?
You promised Smile | :)
GeneralRe: Can't execute this sample (Windows XP) Pin
pappe8219-Jan-11 6:17
pappe8219-Jan-11 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.