Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all available printer drivers like the Add Printer Wizard in C#?

Tags:

c#

printing

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)

enter image description here

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

EDIT: A little background:

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.

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.

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

like image 478
Daniel Frear Avatar asked Mar 20 '12 17:03

Daniel Frear


1 Answers

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

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

Also see WMI Queries topic on MSDN.

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.

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.

like image 163
JamieSee Avatar answered Sep 28 '22 07:09

JamieSee