user2655078
user2655078

Reputation: 1

C# send WMI results to a textbox on a Windows GUI Form

I’m trying to write a small Windows Forms GUI that would take in the text of a WMI query, and have the output/results of that WMI query displayed inside a textbox on the Form.

For testing purposes to prove things are working, I’m trying to get the GUI to write the WMI output to the command line Console, but I’m having no luck with displaying the output so far.

Where am I going wrong (I'm new to C#, so that's gonna be a long list!)?

This is the code behind the Form I'm working with...

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

namespace WMI_Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_runQuery_Click(object sender, EventArgs e)
        {
            string _userName = textBox1_userName.Text;
            string _passWord = textBox2_password.Text;
            string _serverName = textBox3_serverName.Text;
            string _wmiQuery = textBox4_queryInput.Text;

            EnumServices(_serverName, _userName, _passWord);
        }

        static void EnumServices(string host, string username, string password)
        {
            string ns = @"root\cimv2";
            string query = "SELECT * FROM Win32_LogicalDisk";
            //string query = "select * from Win32_Service";

            ConnectionOptions options = new ConnectionOptions();
            if (!string.IsNullOrEmpty(username))
            {
                options.Username = username;
                options.Password = password;
            }

            ManagementScope scope =
                new ManagementScope(string.Format(@"\\{0}\{1}", host, ns), options);
            scope.Connect();

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));

            ManagementObjectCollection retObjectCollection = searcher.Get();

            foreach (ManagementObject mo in searcher.Get())
            {
                 Console.WriteLine("Trying to output the results...");
                 Console.WriteLine(mo.GetText(TextFormat.Mof));     
            }  
        }
    }
}

Upvotes: 0

Views: 1618

Answers (1)

Colin Smith
Colin Smith

Reputation: 12540

Because your project is a "Windows" application and not a "Console" application, you don't have a displayed/attached console window....thus the Console.WriteLine output has nowhere to go.

Instead of going through the trouble of creating a "console" for your GUI application (e.g. via AllocConsole - http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528(v=vs.85).aspx) which would then allow your Console.WriteLine output to be seen....it's not necessary in this case...as eventually you will be outputting into a ListBox...and you just want a quick way to "see" the data.

The quickest way to do that is with a "Trace" or "Debug" output statement:

(What’s the difference between the Debug class and Trace class?):

So:

System.Diagnostics.Trace.WriteLine("Trying to output the results...");
System.Diagnostics.Trace.WriteLine(mo.GetText(TextFormat.Mof));

or

System.Diagnostics.Debug.WriteLine("Trying to output the results...");
System.Diagnostics.Debug.WriteLine(mo.GetText(TextFormat.Mof));

The output will then appear in your "Output" window of Visual Studio if you run the "debug" build of your program from it.

If you start your program outside of Visual Studio, then you can use DebugView (http://technet.microsoft.com/en-gb/sysinternals/bb896647.aspx) to see the debug output.

After you have confirmed it's working, you can then put in the logic to add the output into a ListBox instead.

Upvotes: 1

Related Questions