C#打印机属性WMI [英] c# printer properties WMI
本文介绍了C#打印机属性WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我有这个code中检索打印机属性:
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
PrinterState:0
PrinterStatus:3
PrinterStatus:3
基本上,我需要这个检查打印机是否缺纸。我认为应该是:PrinterState:4
Basically I need this to check if printer is out of paper. What I think would be: PrinterState: 4
在测试了WXP-86和w7-64返回相同的,.NET 4.0中
Tested on wxp-86 and w7-64 return the same, .Net 4.0
感谢你。
推荐答案
据的 MSDN ,缺纸= 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);
}
}
}
}
这篇关于C#打印机属性WMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文