C#/.NET: How to add networked printer to a local PC account?

Viewed 6448

I'm using the WMI Code Creator to create code to add a networked printer.

http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png

The code that was generated works great (under my domain account anyway):

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementClass classInstance =
                    new ManagementClass("root\\CIMV2",
                    "Win32_Printer", null);

                // Obtain in-parameters for the method
                ManagementBaseObject inParams =
                    classInstance.GetMethodParameters("AddPrinterConnection");

                // Add the input parameters.
                inParams["Name"] =  "\\\\PrintServer\\PrinterName";

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("AddPrinterConnection", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

However, I need to add a networked printer to a local PC account, ie a non-domain account that does not have access to \PrintServer.

Where can I put in a domain user's (a service account) username and password into the above code?

I have been Googling for hours but all I can find is that one stupid post that says how to add a printer on a remote machine, which is not what I'm looking to do.

(I need to add a remote printer to the current PC, not to a remote PC.) (The caveat is that the logged on user is a local PC account.)

Does anyone know how this can be achieved?

2 Answers

So since other answer was removed. I just tested this and it worked. Here is link to get the class I am using https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User Here is the code below which worked for me. If you look there are some even better implementations of classes out there to impersonate users.

    static void Main(string[] args)
    {

        using (new Impersonator("username", "domainName", "myPassword"))
        {
            // The following code is executed under the impersonated user.
            AddPrinterUnc(@"\\PrintServer\printershare");
        }


    }

    public static void AddPrinterUnc(string printUncPath)
    {

            using (ManagementClass oPrinterClass = new ManagementClass(new ManagementPath("Win32_printer"), null))
            {
                using (ManagementBaseObject oInputParameters = oPrinterClass.GetMethodParameters("AddPrinterConnection"))
                {
                    oInputParameters.SetPropertyValue("Name", printUncPath);

                    oPrinterClass.InvokeMethod("AddPrinterConnection", oInputParameters, null);

                }
            }

    }
Related