如何在文本框中显示基于控制台的查询的结果?

时间:2016-05-31 18:40:12

标签: c# console wmi-query

我正在尝试使用此控制台代码并在名为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);
        }
    }

1 个答案:

答案 0 :(得分:0)

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: " );
        }
    }
}
}