public PrinterDriverInstallPage()
{
InitializeComponent();
}
private void PrinterDriverDetectButton_OnClick(object sender, RoutedEventArgs e)
{
// string dSQL = "select * from Win32_SystemDriver";
string printerDriverName = "";
string isInstalled = "";
string wmiSQL = "SELECT * FROM Win32_PnPSignedDriver";
// ManagementObjectCollection pnpCollection = new ManagementObjectSearcher(wmiSQL).Get();
List<PrinterDriverInfo> items = new List<PrinterDriverInfo>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiSQL);
foreach (ManagementObject printer in searcher.Get())
{
if (printer["DeviceID"].ToString().ToLower().StartsWith("usbprint"))
{
if (printer["IsSigned"].ToString().ToLower().Equals("true"))
{
printerDriverName = printer["DeviceID"].ToString().Split('\\')[1];
printerDriverNames.Add(printerDriverName);
isInstalled = "已安装";
isInstalleds.Add(isInstalled);
items.Add(new PrinterDriverInfo(){PrinterDriverName = printerDriverName, IsInstalled = isInstalled});
}else
{
printerDriverName = printer["DeviceID"].ToString().Split('\\')[1];
printerDriverNames.Add(printerDriverName);
isInstalled = "未安装";
isInstalleds.Add(isInstalled);
items.Add(new PrinterDriverInfo(){PrinterDriverName = printerDriverName, IsInstalled = isInstalled});
}
}
}
printerDriverInfoList.ItemsSource = items;
}
打日志,多半是这句出了问题
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiSQL);
直接执行PowerShell脚本也可以测试:Get-CimInstance Win32_PnPSignedDriver | ? {$_.DeviceID -match "usbprint"}
看看windows日志里后啥?
加个try catch 调试一下。
我这边也遇到类似的问题:wpf 实现打印功能,在Win10系统没有问题,在Win7系统就报错了,报错如下: PrintTicket 提供程序无法检索 PrintCapabilities。Win32 错误-2147467259
– C#小学徒 4个月前