views:

524

answers:

1

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;

}
A: 
  ManagementPath mPath = new ManagementPath() ;
        mPath.NamespacePath = "root\\cimv2";
        mPath.Server = Server.TrimStart(new char[] { '\\' });
        mPath.RelativePath = "Win32_Printer.DeviceID=\"" + Name + "\"";
        ManagementObject Printer = new ManagementObject(mPath);
        string oldname = Printer.Properties["PortName"].Value.ToString();
        Printer.Properties["PortName"].Value = PortName;
        Printer.Put();

this one works faster, although I think it can be improved further.

Alexander Taran