本文介绍了c#打印机属性WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我有这个代码来检索打印机属性:
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:
打印机状态:0
打印机状态: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!