次のコードを使用して、プリンタの「ポート」プロパティを変更します。問題は、1分より長く実行されることです。それをスピードアップする方法はありますか?wmiオブジェクトのすべてのプロパティを使用せずに管理オブジェクトをインスタンス化できますか?さらに重要なことに、1つのプロパティのみを更新するにはどうすればよいですか?たぶん私はサーチャーなしでmanagementobjectをインスタンス化する必要がありますか?
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;
}