I'm trying to update some information on some printers on a remote server. I need to update stuff like location, comment and port. I have a solution that works, but I find it very slow, and I was wondering if anyone have any idea of why.
I get the printer (and later the port) from the server through WMI with a code like this: (this is testing code)
var test = DateTime.Now;
ManagementScope scope3 = new ManagementScope("\\\\printserver\\root\\cimv2");
scope3.Connect();
SelectQuery q3 = new SelectQuery("select * from Win32_Printer WHERE Name = 'printername'");
ManagementObjectSearcher search3 = new ManagementObjectSearcher(scope3, q3);
var printers3 = search3.Get();
foreach(var p in printers3)
{
//do stuff with printer here.
}
var test2 = DateTime.Now.Subtract(test).TotalSeconds;
When it is done test2 will contain "33.something" seconds. If I do it without the where clause, it will take almost the same time. Admittedly there are almost 1500 printers on this server, but I feel like I should be able to query for one specific printer in a faster way, and I do not understand why a query with a where clause on the name of the printer takes the same time as a "select all" query.
Any suggestions?
--
Update
As suggested below, I have tried to run the same query multiple times. Still takes the same amount of time. It just feels weird to me, that Windows would need to "touch" every single printer on the system, when I'm searching for a specific one.