Installing a printer using ManagementClass gives me an Unknown Error RRS feed

  • Question

  • I'm new at c#.

    I'm trying to install a printer programatically using the post I found in stackoverfow however I get an unknown error.

    Here is my code:

    static public string addprinter()
                ManagementClass win32Printer = new ManagementClass("Win32_Printer");
                ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection");
                inputParam.SetPropertyValue("Name", "\\\\");

                ManagementBaseObject result = (ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null);
                uint errorCode = (uint)result.Properties["returnValue"].Value;
                switch (errorCode)
                    case 0:
                        return ("Successfully connected printer.");

                    case 5:
                        return ("Access Denied.");

                    case 123:
                        return ("The filename, directory name, or volume label syntax is incorrect.");

                    case 1801:
                        return ("Invalid Printer Name.");

                    case 1930:
                        return ("Incompatible Printer Driver.");

                    case 3019:
                        return ("The specified printer driver was not found on the system and needs to be downloaded.");

                return ("Unkown error.");

    And here is the form that calls the code;

    private void button3_Click(object sender, EventArgs e)
                if (true)


    I wan't to add the network printers to my pc. (Example: Printer model name: Samsung ML-371x Series, Printer adress= But I don't know how this code install Samsung ML-371x Series driver to pc. I have all the printer drivers in my pc.


    Wednesday, August 28, 2019 9:46 AM

All replies

  • Your printer does not speak HTTP.  The server name should be "\\\\".

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, August 28, 2019 8:19 PM
  • Hi Craedra, 

    Thank you for posting here.

    Could you provide the value of ‘errorCode’ you get?

    As Tim Roberts suggested, I use ‘\\\\’ to make a test.

    I get the following errorCode:

    I find that the number means your RPC server is unavailable.

    You can find error codes in the following link.

    System Error Codes (1700-3999)

    If you get the same error code, I hope the following reference can help you.

    WMI The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, August 30, 2019 6:35 AM