我正在尝试编写一个小型 Windows 窗体 GUI,它将接收 WMI 查询的文本,并将该 WMI 查询的输出/结果显示在窗体上的文本框中。
出于测试目的以证明一切正常,我试图让 GUI 将 WMI 输出写入命令行控制台,但到目前为止我没有显示输出的运气。
我哪里错了(我是 C# 新手,所以这将是一个很长的列表!)?
这是我正在使用的表单背后的代码......
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));
}
}
}
}