title: C# send WMI results to a textbox on a Windows GUI Form
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Where am I going wrong (I'm new to C#, so that's gonna be a long list!)?</p>
  
  <p><strong>This is the code behind the Form I'm working with...</strong></p>
  
  <pre><code>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));     
              }  
          }
      }
  }
  </code></pre>