//--set the scope of this search to the local machine ManagementScope scope = new ManagementScope(@"\root\cimv2"); //--connect to the machine scope.Connect(); //--query for the ManagementObjectSearcher to get all printer SelectQuery selectQuery1 = new SelectQuery(); selectQuery1.QueryString = "Select * from win32_Printer"; ManagementObjectSearcher obj = new ManagementObjectSearcher(scope, selectQuery1); ManagementObjectCollection printers = obj.Get(); //--now loop through each printer instance returned foreach (ManagementObject printer in printers) { //--first make sure that there is a printer if (printer != null) { str = printer["Name"].ToString().ToLower(); //--check if it matches the name provided if (str.ToUpper().Equals("MyPrinterName".ToUpper())) { //--since we found a match check it's status if (printer["WorkOffline"].ToString().ToLower().Equals("true") || printer["PrinterStatus"].Equals(7)) { //--it's offline online = false; } else { //--it's online online = true; } } } else { throw new Exception("No printers were found"); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)