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?