title: Formatting USB Drives programmatically
body: <p>I am developing an application in C# so whereby, if the user confirms a messagebox to formatting a USB drive, selected from a combobox list, the drive will be formatted. </p>
  
  <p>I haven't got an idea how to approach this, however - I have the following code:</p>
  
  <pre><code> public static bool FormatDrive(string driveLetter,
      string fileSystem = "FAT", bool quickFormat = false,
      int clusterSize = 4096, 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;
      } 
  </code></pre>
  
  <p>I'm not really sure how <a href="http://www.codeproject.com/Articles/115598/Formatting-a-Drive-using-C-and-WMI" rel="nofollow noreferrer">this</a> works. Is this the correct way to approach formatting USB Drives? If not, could someone point me in the right direction?</p>
  
  <p>I have tried looking at the <code>Win32_Volume</code> class but, again, I don't really understand how it works. <a href="https://stackoverflow.com/questions/3320744/usb-drive-programming-with-net">This</a> question would suggest using the <code>CreateFile</code> function. I have also looked at <a href="http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html" rel="nofollow noreferrer">this</a> website.</p>
  
  <p>Any tips of pushing me into the right direction, would be much appreciated.</p>