We help IT Professionals succeed at work.

Error getting INF file using WMI commads

samelamin
samelamin used Ask the Experts™
on
Hey all

I am trying to retreive the INF file for a installed printer driver

The aim is to uise it to instal clones of the same printer, however I am getting an error and i dont know why because it gives me the name of the printer


the error:
Object reference not set to an instance of an object.
string printerName, printerDriver, portName; bool sharedPrinter;
            string path="not working";
            string infname = "";
            portName = null;

            chosen = comboBox1.SelectedItem.ToString();

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PrinterDriver WHERE NAME='"+chosen+"'");
            ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(objectQuery);

            foreach (ManagementObject mo in searcher.Get())
           
                {
                    infname = mo["InfName"].ToString();
                }

            MessageBox.Show(infname);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
which line throws the error?
Meir RivkinFull stack Software Engineer

Commented:
it's possible that mo["InfName"] returns null and then u call ToString() which ultimately trigger the exception.
other than that u should debug it to see what code throw the exception

Author

Commented:
Thats right thats what triggers the error


So if its null does that mean that the INF is ntprint.inf?

I thought its supposed to return it regardless?

how should i resolve this?
Meir RivkinFull stack Software Engineer

Commented:
according to Microsoft Windows Printing Team the InfName field is IN only, used for installing a driver. You cannot retrieve it. (http://www.pcreview.co.uk/forums/thread-531326.php)

Author

Commented:
Wow ok that clears things up.

So is there any way to add a new printer using the drivers already installed in code ?
Full stack Software Engineer
Commented:
you should use rundll32 printui.dll,PrintUIEntry
check http://support.microsoft.com/kb/189105

Author

Commented:
ALright I see where my mistake is

I am trying to add a printer but at the same time i want that printer to use a driver thats already installed

So installing the print driver was a mistake the aim is to add a printer then allocate it adriver thats already been installed

I dont think i can do that using print ui

Author

Commented:
solved it thanks