我正在尝试获取网络打印机的状态。正如这个链接所说,我尝试了 WMI 代码。但是即使我拔掉了网线,它总是会转到其他部分。
这是我尝试过的代码:
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_Printer");
string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
if (printer["WorkOffline"].ToString().ToLower().Equals("true") && printer["Default"].ToString().ToLower().Equals("true"))
{
string s = "Printer offline" + printerName;
listBox1.Items.Add(s);
}
else
{
// printer is not offline
// Console.WriteLine("Your Plug-N-Play printer is connected.");
string s = "Printer found and is online " + printerName;
listBox1.Items.Add(s);
}