Trouble adding WMI code to C# application

Hi,

I'm not sure where to begin but I am a system adminstrator who runs vb scripts for simple tasks. I would like to change these scripts from a command prompt to a nice gui screen for the other admins to use. Microsoft published the WMI Code Creator which was very helpful in making the code in Visual Basic Script but not sure how to use the C# version

Basically I tried to enter this code into a very simple Windows form and made a single button to execute it.

Can someone point me in the right direction :)

Here's the code in Visual Basic Script. Just checks for the Application Management service start mode.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Service WHERE Name = 'AppMgmt'",,48)
For Each objItem in colItems
Wscript.Echo "-----------------------------------"
Wscript.Echo "Win32_Service instance"
Wscript.Echo "-----------------------------------"
Wscript.Echo "StartMode: " & objItem.StartMode
Next

Here's the code in C# which I want to use.

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Service WHERE Name = 'AppMgmt'");

foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_Service instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("StartMode: {0}", queryObj["StartMode"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}

Any comments are welcome. I'm going over the Express vidoes but didn't see this topic covered.

Thanks,

Rob



Answer this question

Trouble adding WMI code to C# application

  • ShawnMullen

  • Whoisit

    the same thing applies for GUI mode, only difference is to display the results, instead of Console.Write() just place it in a textbox or show a messagebox. So create a new Windows Application, add the System.Management.dll reference and import the namespace in the using statement/declaratives then drag a button on the form and place the main code in the button click event.

    Example:

    private void button1_click(object sender, EventArgs e)

    {

       try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_Service WHERE Name = 'AppMgmt'");

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        MessageBox.Show("StartMode: " + queryObj["StartMode"].ToString());
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }

    }



  • Christopher M. Keslin

    I tried adding this to my code but received the following error.

    "The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference )"

    I'll be searching the MSDN documents to see if I can get this resolved. Here's my code so far.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Management;

    using System.Text;

    using System.Windows.Forms;

    namespace WMItest

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Test");

    try

    {

    ManagementObjectSearcher searcher =

    new ManagementObjectSearcher("root\\CIMV2",

    "SELECT * FROM Win32_Service WHERE Name = 'AppMgmt'");

    foreach (ManagementObject queryObj in searcher.Get())

    {

    MessageBox.Show("StartMode: " + queryObj["StartMode"].ToString());

    }

    }

    catch (ManagementException e)

    {

    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);

    }

    }

    }

    }


  • Chris Boorman

    right click references in your project and go to add reference then select System.Management :-)

  • Drudkh

    Thanks so much, got it working!

    If I wanted to find more information about the System.Management where would I find this

    Thanks,
    Rob

  • Large_Goose

    Hey Rob,

    I just glanced over your code. What's the issue you're having Are you getting the results you expect

    Regards,

    Mike


  • Trouble adding WMI code to C# application