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

发布于 2024-07-14 02:41:35 字数 920 浏览 2 评论 0原文

我使用以下代码来更改打印机“端口”属性。 问题是它执行的时间超过一分钟。 有办法加快速度吗? 我可以不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-07-21 02:41:35
  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();

这个运行速度更快,尽管我认为它可以进一步改进。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文