c# - RegistryKey ValueCount/SubKeyCount 错误

标签 c# registry

我正在尝试查询以下注册表键值:

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

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

HKLM\SOFTWARE\Microsoft\MSSQLServer\Client HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\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();
    }

最佳答案

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

在 Windows x64 上,当您指定(在代码中)“Software\Microsoft\MSSQLServer\Client”

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

Visual Studio 2010 默认以 x86 模式创建 exe,请注意此提示。

关于c# - RegistryKey ValueCount/SubKeyCount 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2885097/

相关文章:

c# - 无需缩进的 WPF TreeView

c# - 带有文档索引作为附件的FileSystemwatcher

c++ - 带有 HKEY_LOCAL_MACHINE 的 RegOpenKeyExW 在 Windows Embedded 7 64 位上返回 2

注册表脚本 - 您可以在不使用 dword 关键字的情况下分配十进制值吗

c# - 更改 EnableDesktopModeAutoInvoke 注册表值并使 TabTip.exe 接受更改

c# - 无法确定要使用的参数规范

c# - Caliburn.Micro,MVVM 模式 : CanExecute command doesn't work

c++ - HKEY 句柄为 NULL,在调试期间它写入未使用的<无法读取内存>

C++ 搜索 Windows 注册表

c# - 如何在 C# 中将名字和姓氏的首字母大写?