title: c# check printer status
body: <p>in my application (Windows 7, VS2010) i have to decrement a credit counter after successfully printing an image.
  Anyway, before starting the entire process, i'd like to know about printer status in order to alert the user on paper out, paper jam and so on.
  Now, looking around i found several example that use Windows WMI but... never works. Using <a href="https://stackoverflow.com/questions/887785/talking-to-a-printer">THIS</a> snippet, for example, the printer status is always ready also if i remove the paper, open the cover... turn off the printer.</p>
  
  <p>The printer status is always good also now, that i'm testing from office the printer that is comfortably turned off at home.
  have I to detonate the device by dynamite in order to have a printer error status?</p>
  
  <p>This is the code i've used</p>
  
  <pre><code>ManagementObjectCollection MgmtCollection;
  ManagementObjectSearcher MgmtSearcher;
  
  //Perform the search for printers and return the listing as a collection
  MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");
  MgmtCollection = MgmtSearcher.Get();
  
  foreach (ManagementObject objWMI in MgmtCollection)
  {
  
      string name = objWMI["Name"].ToString().ToLower();
  
      if (name.Equals(printerName.ToLower()))
      {
  
          int state = Int32.Parse(objWMI["ExtendedPrinterStatus"].ToString());
          if ((state == 1) || //Other
          (state == 2) || //Unknown
          (state == 7) || //Offline
          (state == 9) || //error
          (state == 11) //Not Available
          )
          {
          throw new ApplicationException("hope you are finally offline");
          }
  
          state = Int32.Parse(objWMI["DetectedErrorState"].ToString());
          if (state != 2) //No error
          {
          throw new ApplicationException("hope you are finally offline");
          }
  
      }
  
  }
  </code></pre>
  
  <p>Where 'printerName' is received as parameter.</p>
  
  <p>Thank you in advice.</p>