gpt4 book ai didi

c# - RegistryKey ValueCount/SubKeyCount 错误

转载 作者:行者123 更新时间:2023-12-04 02:23:32 26 4
gpt4 key购买 nike

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

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

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

HKLM\SOFTWARE\Microsoft\MSSQLServer\ClientHKLM\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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com