2010-05-21 47 views
2

我想查詢以下注冊表項值:的RegistryKey ValueCount/SubKeyCount錯誤

HKLM \ SOFTWARE \微軟\的MSSQLServer \客戶端\ SharedMemoryOn HKLM \ SOFTWARE \微軟\的MSSQLServer \客戶端\ SuperSocketNetLib \ ProtocolOrder

但取決於我運行程序的機器查詢返回null。當調試我的本地機器上,我檢查值ValueCount爲:

HKLM \ SOFTWARE \微軟\的MSSQLServer \客戶 HKLM \ SOFTWARE \微軟\的MSSQLServer \客戶端\ SuperSocketNetLib

計數爲0, OpenSubKey返回null。

我是一個域管理員,本地管理員組中添加以下到我的app.manifest:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

任何想法,爲什麼?

 private static void ValidateSqlClientSettings() 
    { 
     Console.WriteLine("\r\n/////////////// LOCAL SQL CLIENT PROTOCOLS ////////////////"); 

     RegistryKey keyHKLM = Registry.LocalMachine; 
     ///TODO: nullreferenceexception - connect to remote machine and find out why 
     RegistryKey sqlClientKey = keyHKLM.OpenSubKey(@"SOFTWARE\Microsoft\MSSQLServer\Client"); 

     if (sqlClientKey == null) 
     { 
      WriteLine2Console(@"WARNING: unable to read registry key '{0}\SOFTWARE\Microsoft\MSSQLServer\Client'", ConsoleColor.Yellow); 
     } 

     var cliKeyNames = from k in sqlClientKey.GetSubKeyNames() 
          where k == "SuperSocketNetLib" 
          select k; 

     ///TODO: find out why these values are always missing (even if I can see them in regedit) 
     Console.Write("Shared Memory Disabled (cliconfg): "); 
     if (Convert.ToBoolean(sqlClientKey.GetValue("SharedMemoryOn"))) 
      WriteLine2Console("FAILED", ConsoleColor.Red); 
     else if(sqlClientKey.GetValue("SharedMemoryOn") == null) 
      WriteLine2Console(String.Format("WARNING - unable to read '{0}\\SharedMemoryOn'", sqlClientKey.Name), ConsoleColor.Yellow); 
     else 
      WriteLine2Console("PASS", ConsoleColor.Green); 

     Console.Write("Client Protocol Order (cliconfg - tcp first): "); 
     foreach (string cliKey in cliKeyNames) 
     { 
      RegistryKey subKey = sqlClientKey.OpenSubKey(cliKey); 
      object order = subKey.GetValue("ProtocolOrder"); 
      if (order != null && order.ToString().StartsWith("tcp") == false) 
      { 
       WriteLine2Console("FAILED", ConsoleColor.Red); 
      } 
      else if (order == null) 
      { 
       WriteLine2Console(String.Format("WARNING - unable to read '{0}\\ProtocolOrder'", subKey.Name), ConsoleColor.Yellow); 
      } 
      else 
      { 
       WriteLine2Console("PASS", ConsoleColor.Green); 
      } 
      subKey.Close(); 
     } 

     sqlClientKey.Close(); 
     keyHKLM.Close(); 
    } 

回答

4

你要注意的另一個因素是應用位數。 (在代碼中)「Software \ Microsoft \ MSSQLServer \ Client」中指定(在註冊表編輯器)「SOFTWARE \ WOW6432Node \ Microsoft \ MSSQLServer \ Client」下的Windows註冊表項。

這是註冊表項的WOW64重定向。

由於默認情況下Visual Studio 2010在x86模式下創建exe,因此您應該注意這一提示。

+0

謝謝!我將我的平臺目標更改爲「任何CPU」,並且工作正常。我還沒有在x86機器上試過這個結果,所以這可能對驗證也很重要。但是我之前沒有注意到x86的默認設置。 – 2010-06-02 15:17:06

0

試着改變你的sqlclientkey以下幾點:

RegistryKey sqlClientKey = keyHKLM.OpenSubKey(@"SOFTWARE\\Microsoft\\MSSQLServer\\Client"); 
相關問題