C#将WMI结果发送到Windows GUI表单上的文本框
我正在尝试编写一个小的Windows窗体GUI,它将接收WMI查询的文本,并将WMI查询的输出/结果显示在窗体的文本框中 为了测试以证明一切正常,我试图让GUI将WMI输出写入命令行控制台,但到目前为止,我还没有显示输出的机会 我哪里做错了(我是C#的新手,所以这将是一个很长的列表!) 这是我正在使用的表单背后的代码…C#将WMI结果发送到Windows GUI表单上的文本框,c#,winforms,forms,wmi,C#,Winforms,Forms,Wmi,我正在尝试编写一个小的Windows窗体GUI,它将接收WMI查询的文本,并将WMI查询的输出/结果显示在窗体的文本框中 为了测试以证明一切正常,我试图让GUI将WMI输出写入命令行控制台,但到目前为止,我还没有显示输出的机会 我哪里做错了(我是C#的新手,所以这将是一个很长的列表!) 这是我正在使用的表单背后的代码… using System; using System.Collections.Generic; using System.ComponentModel; using System
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));
}
}
}
}
因为您的项目是“Windows”应用程序而不是“控制台”应用程序,所以您没有显示/附加的控制台窗口……因此,Console.WriteLine
输出无处可去
而不是麻烦地为GUI应用程序创建“控制台”(例如,通过alloconsole
-),这样就可以查看控制台.WriteLine
输出……在这种情况下,这是不必要的……因为最终您将输出到列表框中……而您只需要快速地“查看”数据
最快的方法是使用“跟踪”或“调试”输出语句:
():
因此:
或
如果从Visual Studio的“输出”窗口运行程序的“调试”版本,则输出将显示在该窗口中
如果在Visual Studio之外启动程序,则可以使用DebugView()查看调试输出
确认其工作后,您可以输入逻辑,将输出添加到
列表框中。好的,不要使用Console.WriteLine(),它不是文本框。例如,将字符串添加到列表框中;科林·史密斯-谢谢你们两位的指导/建议!非常感谢!:-)
System.Diagnostics.Trace.WriteLine("Trying to output the results...");
System.Diagnostics.Trace.WriteLine(mo.GetText(TextFormat.Mof));
System.Diagnostics.Debug.WriteLine("Trying to output the results...");
System.Diagnostics.Debug.WriteLine(mo.GetText(TextFormat.Mof));