如何在 C# 中列出所有可用的打印机驱动程序,如添加打印机向导? [英] How can I list all available printer drivers like the Add Printer Wizard in C#?

查看:100
本文介绍了如何在 C# 中列出所有可用的打印机驱动程序,如添加打印机向导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很可能我必须为此写一些过于复杂的东西,而且没有常规的方法可以做到,但是:

It may well be that I have to write something overly complex for this and that there is no regular way to do it, but:

我怎样才能获得所有可用打印机驱动程序的列表,就像添加打印机向导在按制造商然后按打印机显示它们时所做的那样?我基本上需要一个自定义添加打印机对话框,没有这个,我有点吃不消;)

How can I get myself a list of all available printer drivers much like the add printer wizard does when it displays them by manufacturer then by printer? I basically need a custom add printer dialog and without this, I'm a bit stuffed ;)

像这样:(忽略箭头,借来的图片)

Like this: (ignore the arrow, borrowed image)

我还需要列表中驱动程序表示的 .inf 文件的路径

I would also need the path to the .inf file denoted by the drivers in the list

一点背景:

我试图通过在 TS 会话中调用添加打印机向导(具有提升的权限)来添加打印机,将打印机添加到正确的 TS 端口,然后尝试保存该打印机的信息,以便在登录用户时打印机是根据我保存的值自动添加的.

I am attempting to add a printer by invoking the add printer wizard (with elevated privs) in a TS session, adding the printer to the correct TS Port and then attempting to save the information for that printer so as upon login the users printer is added automatically based on values I've saved.

到目前为止,我还没有找到在添加打印机后获取驱动程序信息的方法(最好是该打印机驱动程序的 .inf 文件的实际路径,因为这样我就可以使用 PrintUI 来安装打印机).

So far, I've not been able to find a way to get the driver information (preferably the actual path to the .inf file for that printer driver as then I can use PrintUI to install the printer) after adding the printer.

因此,我使用自定义对话框来匹配打印机驱动程序.由于该工具仅在初始打印机安装时使用,并且仅由管理员使用,因此我不介意您在第二次选择驱动程序的辅助对话框中保存信息.

As such, I am resorting to a custom dialog to match the printer driver. As the tool is only used upon initial printer install and only by administrators I don't mind having a secondary dialog where you choose the driver a second time just so I can save the information.

针对 Windows Server 2008 R2(Win32_PrinterDriver 似乎不起作用)

Targeting Windows Server 2008 R2 (Win32_PrinterDriver doesnt appear to work)

推荐答案

您可以查询 WMI 以获取有关当前正在使用的打印机驱动程序的信息.Win32_PrinterDriver 类 详细说明可用的属性.您可以使用 System.Management Namespace 中的类来执行查询.

You can query WMI for information on the printer drivers that are currently in use. The Win32_PrinterDriver class details the available properties. You can use classes in the System.Management Namespace to perform the queries.

SelectQuery selectQuery = new SelectQuery("Win32_PrinterDriver");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

foreach (ManagementObject printerDriver in searcher.Get()) 
{
    // Your code here.
}

您可以通过索引来访问属性,即printerDriver["DriverPath"].

You can access the properties by indexing to them, i.e. printerDriver["DriverPath"].

另请参阅 WMI 查询 MSDN 上的主题.

Also see WMI Queries topic on MSDN.

一个有趣的旁注,微软已经添加了一个 Get-PrinterDriver commandlet 到某些版本的 PowerShell(在 Windows 10 等上),其执行的操作与上述代码非常相似.

On an interesting side note, Microsoft has since added a Get-PrinterDriver commandlet to some versions of PowerShell (on Windows 10, etc.) that does something pretty similar to the above code.

更新:我在浏览旧问题时发现了 GitHub 上的 DriverStoreExplorer 项目.枚举所有打印机驱动程序的代码有很多,所以在这里重现它没有意义.

UPDATE: I was looking through old questions and discovered the DriverStoreExplorer project on GitHub. There's a lot to the code that enumerates all printer drivers, so it does not make sense to reproduce it here.

这篇关于如何在 C# 中列出所有可用的打印机驱动程序,如添加打印机向导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆