更新 win32_printer 管理对象需要很长时间
我使用以下代码来更改打印机“端口”属性。 问题是它执行的时间超过一分钟。 有办法加快速度吗? 我可以不使用 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个运行速度更快,尽管我认为它可以进一步改进。
this one works faster, although I think it can be improved further.