更新win32_printer管理对象需要很长时间

时间:2009-02-25 09:30:16

标签: wmi performance

我使用以下代码来更改打印机“port”属性。问题是它执行的时间超过一分钟。 有没有办法加快速度? 我可以使用wmi对象的所有属性来实例化管理对象吗?更重要的是,我怎样才能更新1个房产? 也许我应该在没有搜索者的情况下实例化管理对象?

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;

}

1 个答案:

答案 0 :(得分: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();

这个更快,但我认为它可以进一步改进。