RegistryKey ValueCount/SubKeyCount wrong

Posted by Mark J Miller on Stack Overflow See other posts from Stack Overflow or by Mark J Miller
Published on 2010-05-21T19:45:52Z Indexed on 2010/05/21 19:50 UTC
Read the original article Hit count: 442

Filed under:

I am trying to query the following registry key values:

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

But depending on which machine I'm running the program the query returns null. When I debug on my local machine and I inspect the value for ValueCount for:

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

The count is 0 and OpenSubKey returns null.

I am a domain admin, in the local administrators group and have added the following to my app.manifest:

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

Any idea why?

        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();
    }

© Stack Overflow or respective owner

Related posts about registrykey