首页 新闻 会员 周边 捐助

使用C#和WPF开发打印机驱动检测小程序,win11可以,win7就卡住了

0
悬赏园豆:5 [待解决问题]

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;
        
    }

cswali的主页 cswali | 初学一级 | 园豆:197
提问于:2022-05-20 13:14

我这边也遇到类似的问题:wpf 实现打印功能,在Win10系统没有问题,在Win7系统就报错了,报错如下: PrintTicket 提供程序无法检索 PrintCapabilities。Win32 错误-2147467259

C#小学徒 5个月前
< >
分享
所有回答(3)
0

打日志,多半是这句出了问题

ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiSQL);

 

直接执行PowerShell脚本也可以测试:Get-CimInstance Win32_PnPSignedDriver | ? {$_.DeviceID -match "usbprint"}

talentzemin | 园豆:775 (小虾三级) | 2022-05-20 15:56
1

看看windows日志里后啥?

会长 | 园豆:12463 (专家六级) | 2022-05-20 21:04
0

加个try catch 调试一下。

江宁织造 | 园豆:73 (初学一级) | 2022-06-22 17:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册