WMI in C#[強型別操作]

來源:互聯網
上載者:User

C# 進行WMI操作的內容封裝在System.Management.dll中,具體的在MSDN有詳細描述,預設是用[“**’]去讀取和設定屬性等。

此處介紹一個自動產生強型別的封裝軟體,這樣處理起來就簡單了很多,不用在反覆的查看參數等資訊了。

1、 使用WMIExplorer產生WMI對應的封裝類

2、 針對產生類可以進行強型別操作。

WMIExplorer可以在http://www.primaltools.com/downloads/communitytools/中找到。

如示,選擇需要操作的CIM類別,右鍵產生即可。產生類包括了各種屬性和操作的方法。

以Win32_Process管理機器上的進程為例,針對產生的類的操作方法:

using ROOT.CIMV2.Win32;Process.StaticScope = this.scope;var ps = Process.GetInstances();foreach (var p in ps){using(Process m = p as Process){System.Diagnostics.Debug.WriteLine(m.Name);if (m.Name == "notepad.exe"){m.Terminate(1);}}}uint pid;Process.Create("notepad.exe", null, null, out pid);var my = Process.GetInstances(this.scope, "ProcessId=" + pid.ToString());if (my.Count > 0){var i = my.GetEnumerator();i.MoveNext();Process p = i.Current as Process;p.Terminate(1);}//另一種形式的對比:
ManagementObject mo = new ManagementClass(this.scope, new ManagementPath("MSFT_SIPESUserContactData"), new ObjectGetOptions()).CreateInstance();                    mo["UserInstanceID"] = this.userId;                    mo["SIPURI"] = sip;                    mo["GroupID"] = groupIds;                    mo["Name"] = name;                    mo["Subscribed"] = true;                    mo.Put();                     SIPESUserContactData.StaticScope = this.scope;                    SIPESUserContactData c = SIPESUserContactData.CreateInstance();                    c.LateBoundObject["UserInstanceID"] = this.userId;                    c.LateBoundObject["SIPURI"] = sip;                    c.LateBoundObject["GroupID"] = groupIds;                    c.LateBoundObject["Name"] = name;                    c.LateBoundObject["Subscribed"] = true;                     c.CommitObject();
以上可見,由於產生類中包括了屬性名稱字,即使使用名字時,直接參考類就行了。

方便了很多。

這個例子是操作進程和Office Communicator伺服器上的連絡人的操作樣本等。

http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/WMIDemo.zip

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.