如何查询 Win32_Printer 已安装的远程打印机? (无效查询)
How to query Win32_Printer for installed remote printer? (Invalid 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);
}
}
}
我知道名字了
'\\\\服务器\\打印机名称'
从
System.Drawing.Printing.PrinterSettings.InstalledPrinters.
如果我将查询更改为
"SELECT * from Win32_Printer WHERE Name LIKE '%PrinterName'"
它有效,但我如何查询全名?
正如@JeroenMostert 的评论,'\' 字符在 c# 和 wmi 中都是转义字符,因此解决方案是将它们加倍。
我在尝试从 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);
}
}
}
我知道名字了 '\\\\服务器\\打印机名称' 从 System.Drawing.Printing.PrinterSettings.InstalledPrinters.
如果我将查询更改为
"SELECT * from Win32_Printer WHERE Name LIKE '%PrinterName'"
它有效,但我如何查询全名?
正如@JeroenMostert 的评论,'\' 字符在 c# 和 wmi 中都是转义字符,因此解决方案是将它们加倍。