首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RegistryKey值计数/子键计数错误

RegistryKey值计数/子键计数错误
EN

Stack Overflow用户
提问于 2010-05-22 03:45:53
回答 2查看 1.7K关注 0票数 3

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

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中:

代码语言:javascript
复制
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

知道为什么吗?

代码语言:javascript
复制
        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();
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-22 07:50:20

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

在Windows上,当您(在代码中)指定“x64 \MSSQLServer\Client”时,您的x86构建将在(在regedit中) "SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client",下查找注册表项。

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

由于Visual Studio2010默认情况下在x86模式下创建exe,因此您应该注意本技巧。

票数 6
EN

Stack Overflow用户

发布于 2010-05-22 04:28:09

尝试将sqlclientkey更改为以下内容:

代码语言:javascript
复制
RegistryKey sqlClientKey = keyHKLM.OpenSubKey(@"SOFTWARE\\Microsoft\\MSSQLServer\\Client"); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2885097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档