0

次のコードを使用して、プリンタの「ポート」プロパティを変更します。問題は、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;

}
4

1 に答える 1

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

これはより高速に動作しますが、さらに改善できると思います。

于 2009-02-25T11:56:30.260 に答える