2016-06-02 3 views
0

이 코드에는 누락 된 항목이있는 것 같습니다. 모든 것이 제대로 컴파일되지만, 나는 기대했던 결과를 얻지 못하고있다. 본질적으로 WMIC에서 stipped 코드를 취하여 보호 상태가 0으로 되돌아 오면 텍스트 상자에 "Bitlocker Enabled"가 1이면 "Bitlocker Disabled"를 표시합니다. 그러나 무엇을 넣든 테스트 컴퓨터의 상태와 관계없이 "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 으로 경고받을 수 있나요 것으로 나타났습니다 : "가능한 의도하지 않은 참조 비교, 값 비교를 얻기 위해, 왼쪽 캐스팅 '문자열'을 입력하면됩니다.

+0

예상 한 결과는 무엇입니까? bitLockerCheck.Text는 매번 덮어 쓰여지고 컬렉션의 마지막 값을 표시합니다. – vabii

+0

나는 그것을 알아낼 마음이 없어 :) –

+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"; } 
관련 문제