2009-02-25 5 views
0

J'utilise le morceau de code suivant pour modifier la propriété "port" de l'imprimante. Le problème est qu'il s'exécute plus d'une minute. Y a-t-il un moyen de l'accélérer? Puis-je instancier l'objet de gestion non avec toutes les propriétés de l'objet wmi? Et plus important encore, comment puis-je mettre à jour seulement 1 propriété? Peut-être que je devrais instancier managementobject withouth searcher?La mise à jour de l'objet de gestion win32_printer prend tellement de temps

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; 

} 

Répondre

0
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(); 

celui-ci fonctionne plus vite, même si je pense qu'il peut être encore améliorée.

Questions connexes