Tuesday, June 7, 2011

WMI Code Example Using C#

The following Code Example is one of many that can be found at:

http://www.planetmps.com/

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

namespace WindowsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string tempstr;
        private void Sink_OnObjectReady(SWbemObject objWbemObject, SWbemNamedValueSet objWbemAsyncContext)
        {
            SWbemObjectSet objset =  objWbemObject.Instances_(0, null);
            System.Collections.IEnumerator objEnum = objset.GetEnumerator();
            while (objEnum.MoveNext())
            {
                SWbemObject obj = (SWbemObject)objEnum.Current;
                SWbemPropertySet propset = obj.Properties_;
                System.Collections.IEnumerator propEnum = propset.GetEnumerator();
                while (propEnum.MoveNext())
                {
                    SWbemProperty prop = (SWbemProperty)propEnum.Current;
                    String Name = prop.Name;
                    string Value = checkme(prop);
                    tempstr = tempstr + Name + " = " + Value + "\n\r";
                }
                MessageBox.Show(tempstr);
                tempstr = "";
                break;
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            SWbemLocator locator = new SWbemLocatorClass();
            SWbemServices svc = locator.ConnectServer(".", "root\\CIMV2", "", "", "", "", 0, null);
            SWbemSink Sink = new SWbemSinkClass();
            svc.GetAsync(Sink, "Win32_Processor", 0, null, null);
            Sink.OnObjectReady += new ISWbemSinkEvents_OnObjectReadyEventHandler(Sink_OnObjectReady);

        }
        private string checkme(SWbemProperty prop)
        {
            String value = "";

            if(prop.get_Value() == null)
            {
                return "";
            }
            else
            {
                if(prop.IsArray == true)
                {
                    Object[] myarray = (Object[])prop.get_Value();
                    for (int x = 0; x < myarray.GetLength(0) - 1; x++)
                    {
                        if (value != "")
                        {
                            value = value + ",";
                        }
                        value = value + myarray[x];
                    }

                    return value;
                }
                else
                {
                    if(prop.CIMType == WbemCimtypeEnum.wbemCimtypeDatetime)
                    {
                        value = prop.get_Value().ToString();
                        if (value != "")
                        {
                            value = value.Substring(4, 2) + "/" + value.Substring(6, 2) + "/" + value.Substring(0, 4) + " " + value.Substring(8, 2) + ":" + value.Substring(10, 2) + ":" + value.Substring(12, 2);
                        }
                        return value;
                    }
                    else
                    {
                        return prop.get_Value().ToString();
                    }
                }
            }
        }
    }

No comments:

Post a Comment