问题
Hello I have this code to retreive 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 I need always return the same:
PrinterState:0
PrinterStatus:3
Basically I need this to check if printer is out of paper. What I think would be: PrinterState: 4
Tested on wxp-86 and w7-64 return the same, .Net 4.0
Thank you.
回答1:
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);
}
}
}
}
回答2:
I had this problem as well and there is no easy fix to this.
The cause of the problem is that Windows Management Instrumentation (WMI) retrieves the printer information from the spoolsv.exe process. So the reliability of the information retrieved depends completely on the printer driver. It is likely that the driver of the printer you are querying information for is either bypassing the spooler to get the status or it does not report the status to the spooler process.
Win32_Printer will report whatever status is contained in the spooler. So if the spooler reports Ready then it never receives data with a status change as the default is Ready. Win32_Printer just exports this as Idle (PrinterStatus = 3 or PrinterState = 0).
回答3:
this line:
string query = string.Format("SELECT * from Win32_Printer "
+ "WHERE Name LIKE '%{0}'",
printerName);
try call it with % after printername:
string query = string.Format("SELECT * from Win32_Printer "
+ "WHERE Name LIKE '%{0}%'",
printerName);
often printer name is: "[printername] On [port]"
来源:https://stackoverflow.com/questions/14455964/c-sharp-printer-properties-wmi