【问题标题】:How can i display results from a console based query in a textbox?如何在文本框中显示基于控制台的查询的结果?
【发布时间】:2016-05-31 18:40:11
【问题描述】:

我正在尝试获取此控制台代码并在名为 Bitlocker 的文本框中返回结果。只要我在 Admin 中运行,控制台代码似乎就可以正常工作。我以前从未尝试过检索控制台结果并以纯文本形式显示它们,所以我对自己做错了什么感到非常困惑。

我想要的是检索 queryObj 响应并将其显示在文本框中。

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;
using System.Management;
using System.Windows.Forms;



namespace ComplianceCheck_2._0
{



public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();




    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void Main()
    {
        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption",
                "SELECT * FROM Win32_EncryptableVolume");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_EncryptableVolume instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);

                int status;
                status = int.Parse(Console.ReadLine());
                if (status > 0)
                {
                    Bitlocker.Text = "True";
                }
                else ;
                {
                    Bitlocker.Text = "False";
                }


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

【问题讨论】:

  • 那么你的问题到底是什么?
  • 我正在尝试从 queryObj pf Protection Status 中获取最终结果,以在文本框中返回 0 或 1 结果。
  • 你可以在这里看到,我正在尝试使用二进制方法并说如果我们将状态称为前一个控制台行的对象,那么我们可以假设状态大于 0,那么我可以返回“true " 以纯文本形式发送到 bitlocker 文本框,否则返回 false
  • status = int.Parse(Console.ReadLine()); 将读取用户输入。我猜你想检查queryObj 本身的一些值。
  • Bitlocker.Text = queryObj["ProtectionStatus"] == "0" ? "False" : "True"; 我想这就是你想要的??

标签: c# console wmi-query


【解决方案1】:
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;
using System.Windows.Forms;



namespace ComplianceCheck_2._0
{



public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();




    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void Main()
    {

    }

    private void SystemCheck_Click(object sender, System.EventArgs e)
    {
        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption",
                "SELECT * FROM Win32_EncryptableVolume");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_EncryptableVolume instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);

                Bitlocker.Text = queryObj["ProtectionStatus"] == "0" ? "False" : "True";


            }
        }
        catch (ManagementException)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " );
        }
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多