WMI013-WMI學習筆記(十三)——Win32_Desktop

運行結果截圖:


代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Management.Instrumentation;
using System.Management;

namespace Eyuan.WMIStudy.Win32
{
    public partial class fmDesktop : Form
    {
        public fmDesktop()
        {
            InitializeComponent();
        }

        #region 類字段
        private StringBuilder sbDesktopInfo = new StringBuilder();//存儲獲取的信息
        private string strSql = string.Empty;//查詢語句
        private int iMOCount;//獲取的ManagementObject個數
        private string  mopName;//ManagementObject屬性名稱
        #endregion

        /// <summary>
        /// 窗體加載
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fmDesktop_Load(object sender, EventArgs e)
        {
            lbDesktop.HorizontalScrollbar = true;
            //查詢所有的Win32_Desktop管理對象實例的所有屬性、方法
            strSql = "SELECT * FROM Win32_Desktop";
            ManagementObjectSearcher search = new ManagementObjectSearcher(strSql);
            iMOCount = 0;
            //
            ManagementObjectCollection moc=search.Get();          
            foreach (ManagementObject mo in moc)
            {
                sbDesktopInfo.AppendLine("第" + (++iMOCount) + "個Desktop:");                                               
                //獲取ManagementObject的屬性的枚舉
                PropertyDataCollection.PropertyDataEnumerator pde = mo.Properties.GetEnumerator();               
                while (pde.MoveNext())
                {                   
                    mopName = pde.Current.Name;
                    sbDesktopInfo.AppendFormat("{0,-15}:{1}\n", mopName, mo[mopName]);
                }
                sbDesktopInfo.AppendLine("");
            }
            //
            foreach (string strDesktopInfo in sbDesktopInfo.ToString().Split(new string[] { "\n" }, StringSplitOptions.None))
            {
                lbDesktop.Items.Add(strDesktopInfo);
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章