C # WMI Printer Properties

Hello, I have this code to restore printer properties:

string printerName = "PrinterName"; string query = string.Format("SELECT * from Win32_Printer " + "WHERE Name LIKE '%{0}'", printerName); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection coll = searcher.Get(); foreach (ManagementObject printer in coll) { foreach (PropertyData property in printer.Properties) { Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value)); } } 

But properties always need to return the same:

PrinterState: 0

PrinterStatus: 3

Basically, I need this to check if the printer has paper. I would think: PrinterState: 4

Tested on wxp-86 and w7-64, return the same, .Net 4.0

Thank.

+1
c # printing status
Jan 22 '13 at 10:07
source share
3 answers

According to msdn , Paper Out = 5

 using System; using System.Management; using System.Windows.Forms; namespace WMISample { public class MyWMIQuery { public static void Main() { try { string printerName = "PrinterName"; ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Printer " + "WHERE Name LIKE '%{0}'", printerName);); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Win32_Printer instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("PrinterStatus: {0}", queryObj["PrinterStatus"]); } } catch (ManagementException e) { MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); } } } } 
+1
Jan 22 '13 at 10:29
source share

I also had this problem, and it is not easy.

The cause of the problem is that Windows Management Instrumentation (WMI) retrieves printer information from the spoolsv.exe process. Thus, the reliability of the information received depends entirely on the printer driver. The printer driver you are requesting information for is likely either bypassing the print spooler to obtain status, or not reporting the status to the spool queue process.

Win32_Printer will report on the status of the queue manager. Therefore, if the message queue manager is "Ready", it never receives data with the status, because the default is "Ready". Win32_Printer simply exports this as Idle (PrinterStatus = 3 or PrinterState = 0).

+1
Aug 13
source share

this line:

 string query = string.Format("SELECT * from Win32_Printer " + "WHERE Name LIKE '%{0}'", printerName); 

try calling it with% after printername:

 string query = string.Format("SELECT * from Win32_Printer " + "WHERE Name LIKE '%{0}%'", printerName); 

often printer name: "[printername] at [port]"

0
Jan 22 '13 at 10:15
source share



All Articles