using WMI commands to add a printer

samelamin
samelamin used Ask the Experts™
on
Dear All

I would like to add a printer using a WMI command

any ideas

so far i can get a list of drivers using the code below but how do i add a printer?


thye string chosen is the chosen printer i want to install copies of. so forexample if the chosen is an HP printer i want to install copies of this HP
string path="not working";

            chosen = comboBox1.SelectedItem.ToString();

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PrinterDriver");
            ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(objectQuery);

            foreach (ManagementObject mo in searcher.Get())
            {
                if (chosen == mo["Name"].ToString())
                    path = mo["DriverPath"].ToString();

            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

The following code should be what you are looking for to add a printer, replace the printer name in the strcomputer string and the \\PrintServer1\ArtDepartmentPrinter for your environment

M@
strComputer = "atl-ws-01"
Set objWMIService = GetObject( _
    "winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2")
Set objPrinter = objWMIService.Get("Win32_Printer")
errReturn = objPrinter.AddPrinterConnection _
    ("\\PrintServer1\ArtDepartmentPrinter")

Open in new window

Author

Commented:
Is this in c# though

because if i do that i get multiple errors
Are you trying to script it in C#? If not what are you using?

M@

Author

Commented:
yes

Author

Commented:
so far i am trying to make it work using this program but i keep getting error
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

solved it thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial