Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to be able to change a local printer name within c# but using different credentials than the logged on user. From all the information I can find, it appears that WMI is the best route but.... you cannot set username and password credentials for a local computer connection.

Something like this

C#
private void RenamePrinter(string oldName, string newName)
        {
           
                ConnectionOptions options = new ConnectionOptions();
                options.Username = username;
                options.Password = password;
                options.EnablePrivileges = true;
                options.Authority = "ntlmdomain:DomainName";
                ManagementScope scope = new ManagementScope(@"\\.\root\CIMV2", options);
                scope.Connect();
                SelectQuery query = new SelectQuery("SELECT * FROM Win32_Printer");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                using (ManagementObjectCollection queryCollections = searcher.Get())
                {
                    foreach (ManagementObject printer in queryCollections)
                    {
                        if (printer["name"].ToString() == oldName)
                        {
                            {
                                printer.InvokeMethod("RenamePrinter",
                                                 new object[] { newName });
                            }
                            return;
                        }
                    }
                }
            }


What I have tried:

I have attempted to user impersonation as well but it is not used during the WMI section from what I can tell.
Posted
Updated 10-May-17 11:00am
v2
Comments
Patrice T 10-May-17 19:42pm    
Why do you need to change the name of the printer ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900