Using Management Objects to check Online Printers

I have used the Management class for the first time now and I find it very interesting.

First you have to add a reference System.Managemnt to the project.

  1. using System.Management;  
  2. ManagementScope mgmtscope=new ManagementScope(@"\root\cimv2");  
  3. mgmtscope.Connect();  
  4. ManagementObjectSearcher objsearcher=new ManagementObjectSearcher("Select * from Win32_Printer");  
  5. foreach(ManagementObject printer in objsearcher.Get())  
  6. {  
  7.     if(printer["WorkOffline"].ToString().Equals("true"))   
  8.     {  
  9.         printername=printer["Name"].ToString();  
  10.         MessageBox.Show(printername+"-Offline");  
  11.     }  
  12. }   

Hope you have enjoyed this article.