我无法将控制台结果显示在文本框中

时间:2016-06-02 22:07:12

标签: c# visual-studio-2013 textbox console

似乎这段代码遗漏了一些东西。然而,一切都正常编译,我没有得到我期待的结果。基本上我从WMIC采取一些规定的代码并说保护状态是否为0,在文本框中显示“Bitlocker Disabled”,如果是1,则在文本框中显示“Bitlocker Enabled”。但是,无论我放什么,无论测试计算机的状态如何,我都会“启用Bitlocker”。

                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"]);

                    bitLockerCheck.Text = queryObj["ProtectionStatus"] == "1" ? "Bitlocker Disabled" : "Bitlocker Enabled";


                    // if ((string)queryObj["ProtectionStatus"] == "0") { bitLockerCheck.Text = "Bitlocker Disabled"; }
                    //else if ((string)queryObj["ProtectionStatus"] == "1") { bitLockerCheck.Text = "Bitlocker Enabled"; }
                    // else { bitLockerCheck.Text = ""; }

                }
            }
            catch (ManagementException)
            {
                MessageBox.Show("Please Restart the program");
            }

            {

我注意到我在queryObj [“ProtectionStatus”] == 1行下发出警告 我在下面显示: “可能是非预期的参考比较;要进行值比较,请将左侧投射到'string'类型”

1 个答案:

答案 0 :(得分:0)

结果我可以查询最后一行并转换为字符串。然后,为了看起来不错,我将第一个文本框结果隐藏起来仅供管理员使用,并在第二个文本框中显示答案,其中包含我希望用户看到的verbage。

            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"]);

                    bitLockerCheckInvis.Text += string.Format("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);

                    //bitLockerCheck.Text = queryObj["ProtectionStatus"] == "1" ? "Bitlocker Disabled" : "Bitlocker Enabled";
                    // if ((string)queryObj["ProtectionStatus"] == "0") { bitLockerCheck.Text = "Bitlocker Disabled"; }
                    //else if ((string)queryObj["ProtectionStatus"] == "1") { bitLockerCheck.Text = "Bitlocker Enabled"; }
                    // else { bitLockerCheck.Text = ""; }

                }
            }
            catch (ManagementException)
            {
                MessageBox.Show("Please Restart the program to check Administrative Settings");
            }

            {

                if (bitLockerCheckInvis.Text == "ProtectionStatus: 1") { bitLockerCheck.Text += "Bitlocker Enabled"; }
                if (bitLockerCheckInvis.Text == "ProtectionStatus: 0") { bitLockerCheck.Text += "Bitlocker Disabled"; }
                if (bitLockerCheckInvis.Text == "ProtectionStatus:  ") { bitLockerCheck.Text += "Bitlocker Not Available"; }