如何查询Win32_Printer是否已安装远程打印机? (无效查询)

时间:2019-01-17 12:25:00

标签: c# .net wmi-query

尝试从Win32_Printer获取本地安装的远程打印机信息时,出现“无效查询”错误。

string query = "SELECT * from Win32_Printer WHERE Name = '\\\\Server\\PrinterName'";

ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
SelectQuery objQuery = new SelectQuery(query);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objQuery))
using (ManagementObjectCollection coll = searcher.Get())
{
   foreach (ManagementObject printer in coll)
   {
      foreach (PropertyData property in printer.Properties)
         {
            result.Add(property.Name, property.Value);
         }
    }
}

我叫名字 '\\\\ Server \\ PrinterName' 从 System.Drawing.Printing.PrinterSettings.InstalledPrinters。

如果我将查询更改为

"SELECT * from Win32_Printer WHERE Name LIKE '%PrinterName'"

它可以工作,但是我如何查询全名?

1 个答案:

答案 0 :(得分:0)

根据@JeroenMostert的评论,在c#和wmi中,'\'字符都是转义字符,因此解决方案是将它们加倍。