[C#] 調用WMI

  • 14777
  • 0
  • 2008-11-30

[C#] 調用WMI

  1. 第一步:加入參考
    專案→加入參考→.Net→System.Management
    第二步:引用命名空間
    using System.Management;
  2. 第三步:調用WMI Class
    1類別實體化
    例:
          ManagementClass mClass = new ManagementClass("Win32_Share");
    2引用類別方法(Methods)
          例1:
          ManagementBaseObject mMethod = mClass.GetMethodParameters("Create");

          例2:
      
  3. 
    private void button24_Click(object sender, EventArgs e)
            {
                // Get the object on which the method will be invoked
                ManagementClass processClass =new ManagementClass("Win32_Process");
    
                // Get an input parameters object for this method
                ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
    
                // Fill in input parameter values
                inParams["CommandLine"] = "calc.exe";
    
                // Method Options
                InvokeMethodOptions methodOptions = new InvokeMethodOptions(null,System.TimeSpan.MaxValue);
    
                // Execute the method
                ManagementBaseObject outParams =processClass.InvokeMethod("Create",inParams, methodOptions);
    
                // Display results
                // Note: The return code of the method is
                // provided in the "returnValue" property
                // of the outParams object
                MessageBox.Show("Creation of calculator process returned: " + outParams["returnValue"]);
                MessageBox.Show("Process ID: " + outParams["processId"]);
            } 


    3引用類別屬性(Properties)
          例1:利用foreach取得屬性
         
    
            private void button5_Click(object sender, EventArgs e)
            {//獲取CPU編號
                ManagementClass MyClass= new ManagementClass("Win32_Processor");
                ManagementObjectCollection MyCollection =  MyClass.GetInstances();
                String MyInfo="當前系統CPU編號為:";
                string MyCPUID = "";
                foreach (ManagementObject MyObject in MyCollection)
                {
                   MyCPUID= MyObject.Properties["ProcessorId"].Value.ToString();
                   break;
                }
                MyInfo += MyCPUID;
                MessageBox.Show(MyInfo, "訊息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
          
          例2:
    
            private void button1_Click(object sender, EventArgs e)
            {
                ManagementClass mc = new ManagementClass("Win32_SerialPortConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
    
                foreach (ManagementObject mo in moc)
                {
                      // only return MAC Address from first card
    
                       MessageBox.Show (mo["Name"].ToString());
                }
             }
          
                
    

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo