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



这段代码似乎缺少一些东西。一切都编译得很好,但我并没有得到预期的结果。从本质上讲,我从WMIC中获取了一些带点号的代码,并表示如果保护状态返回为0,则在文本框中显示"Bitlocker Disabled",如果是1,则在文字框中显示"Bitlocker Enabled"。然而,无论我放什么,无论测试计算机的状态如何,我都会得到"Bitlocker Enabled"。

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">

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

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

最新更新