Get a list of the local printer to change the IP address of the printer and the default printer

How can I find all local printers on the computer on which the program is running, with a user who does not have administrator rights. I need to reassign the printer IP address and set the default printer. My idea is to use impersonation to do this, but I don't know where to find the printer, and if it is a good solution to use impersonation.

Thanks for any help!

+3
source share
1 answer

I don't think you're lucky with that. Impersonation will not work here and just an exception. You can try this by doing an impersonation and try to open Environment.Domain, this should give you an exception.

- :

ManagementScope mscope = new ManagementScope(@"\root\CIMV2", options);
mscope.Connect();
System.Management.ObjectQuery oQuery = new ObjectQuery("Select * from Win32_TCPIPPrinterPort");
System.Management.ManagementObjectSearcher searcher = new ManagementObjectSearcher(mscope, oQuery);
ManagementObjectCollection moCollection = searcher.Get();

foreach (ManagementObject mo in moCollection)
{
    string name = mo["Name"].ToString();

    if (name.Equals(this.portName))
    {
        System.Threading.Thread.Sleep(10000);
        mo["HostAddress"] = this.printerIP;
        mo.Put();
        Console.WriteLine("Adjusted Printer Port to new IP address " + this.printerIP);
        return true;
    }
}
+2

All Articles