使用System.Management从Printers获取信息相对简单.
//Declare WMI Variables
ManagementObject MgmtObject;
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)
{
//Do whatever action you want with the Printer
}
Run Code Online (Sandbox Code Playgroud)
看看http://msdn.microsoft.com/en-us/library/aa394363.aspx的方法和Win32_Printer的性能.对于你的问题:
//Test whether a Win32_Printer is out of paper or jammed
int state = Int32.Parse(objWMI["PrinterState"]);
if (state == 4) {
//Paper Jam
} else if (state == 5) {
//Paper Out
}
Run Code Online (Sandbox Code Playgroud)