RegistryKey ValueCount/SubKeyCount错误

Mar*_*ler 2 c# registry

我试图查询以下注册表项值:

HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SharedMemoryOn HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\ProtocolOrder

但是根据我正在运行程序的机器,查询返回null.当我在本地计算机上调试时,我检查ValueCount的值:

HKLM\SOFTWARE\Microsoft\MSSQLServer\Client HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib

计数为0,OpenSubKey返回null.

我是本地管理员组中的域管理员,并已将以下内容添加到我的app.manifest:

        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?

        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();
    }
Run Code Online (Sandbox Code Playgroud)

Lex*_* Li 5

你应该注意的另一个因素是应用程序的位数.

在Windows的x64,x86的版本将查找注册表项下(在注册表编辑器)"SOFTWARE\WOW6432Node \微软\的MSSQLServer \客户端",当您指定(代码)"软件\微软\的MSSQLServer \客户端"

这是注册表项的WOW64重定向.

由于Visual Studio 2010默认在x86模式下创建exe,因此您应该注意这个提示.