I use following piece of code to change printers ‘port’ property. Problem is it executes longer than a minute. Is there a way to speed it up? Can i instantiate management object not with all properties of wmi object? And more importantly, how can i update only 1 property? Maybe i should instantiate managementobject withouth searcher?
ManagementPath mPath = new ManagementPath(); mPath.Server = Server.TrimStart(new char[] {'\\'}); mPath.NamespacePath = 'root\\cimv2'; ManagementScope mScope = new ManagementScope(); mScope.Options.Impersonation = ImpersonationLevel.Impersonate; mScope.Path = mPath; SelectQuery sQ = new SelectQuery(); sQ.ClassName = 'Win32_Printer'; //sQ.SelectedProperties.Add('PortName'); //sQ.SelectedProperties.Add('DeviceID'); sQ.Condition = string.Format('Name=\'{0}\'', Name); ManagementObjectSearcher s = new ManagementObjectSearcher(mScope, sQ); foreach (ManagementObject service in s.Get()) { string oldname = service.Properties['PortName'].Value.ToString(); service.Properties['PortName'].Value = PortName; service.Put( ); this.Port = PortName; return true; }
this one works faster, although I think it can be improved further.