首页 新闻 搜索 专区 学院

使用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
< >
分享
所有回答(3)
0

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

ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiSQL);

 

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

luzemin | 园豆:399 (菜鸟二级) | 2022-05-20 15:56
1

看看windows日志里后啥?

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

加个try catch 调试一下。

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