0

I am trying to take this Console code and return a result in a textbox called Bitlocker. The console code seems to work just fine as long as I run in Admin. I have never tried retrieving console results and diplaying them in plain text before so Im super confused as to what I did wrong.

What i want is to retrieve the queryObj response and display it in a textbox.

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);
        }
    }
Joe Pearson
  • 105
  • 10
  • So what exactly is your problem? – Jonathan Carroll May 31 '16 at 18:42
  • I am trying to get the final result from the queryObj pf Protection Status to return the 0 or 1 result in a textbox. – Joe Pearson May 31 '16 at 18:43
  • you can see here, I am trying to use a binary approach and say if we call status the object of the previous console line then we can assume if status is greater than 0 then i can return "true" in plain text to the bitlocker textbox, else return false – Joe Pearson May 31 '16 at 18:44
  • `status = int.Parse(Console.ReadLine());` that's going to read user input. I'm guessing you want to check some value on `queryObj` itself. – Jonesopolis May 31 '16 at 18:44
  • Okay, are you getting any exceptions? What's not working? – Jonathan Carroll May 31 '16 at 18:45
  • no exceptions or errors, simply nothing happens lol. all i want to do it get that query response and display it in plain text to the user via a textbox. – Joe Pearson May 31 '16 at 18:46
  • 2
    `Bitlocker.Text = queryObj["ProtectionStatus"] == "0" ? "False" : "True";` I think this is what you're wanting?? – Jonathan Carroll May 31 '16 at 18:53
  • @JonathanCarroll YES!!!! this is perfect. I didn't realize at first that your code would not work passively so I applied it to the the press of a generic button and BOOM! it works perfectly. Thank you very much man, this just saved me from another couple hours of reading through my C# books – Joe Pearson May 31 '16 at 19:56

1 Answers1

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: " );
        }
    }
}
}
Joe Pearson
  • 105
  • 10