locked
Storing connection strings in the registry RRS feed

  • Question

  • User497225302 posted

    Can someone advise me on the mechanics/code to store connection strings in the registry? Our company will not allow these to be stored in the web.config file... thanks! Here is some code I was instructed to use (in C#... I'm using NO code bebind in vb.net, just some page-level script in vb.net... do I need to convert this to vb.net?) Beyond that I have no idea what to use this file for or how... how to call the class, etc. Thanks for any clues!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    
    namespace RegistryUtilities
    {
        /// <summary>
        /// This class provides access to appplication dynamic valaues that would
        /// change across different environments (DEV TST PREPROD, etc)
        /// </summary>
        public class GetRegistryKeyValue
        {
    
           public enum RegistryHive
           {
               HKLM, // HKEY_LOCAL_MACHINE    
               HKCR, // HKEY_CLASSES_ROOT
               HKCU, // HKEY_CURRENT_USER    
               HKU, //HKEY_USERS
               HKCC //HKEY_CURRENT_CONFIG
           }
    
           private const string COLON_DELIMITER  = ":";
           private const string COMMA_DELIMITER = ",";
           private string BACKSLASH_DELIMITER = "\\";
           private const string REGISTRY_PREFIX = "registry:";
           // Receives a string in the format:    
           // registry:HKLM\Software\ASP.NET\MyKey\ASPNET_SETREG,sqlConnectionString    
           // and pulls the value from the correct registry hive, and extracts and    
           // decrypts the connection string information    
           
            /// <summary>
            /// The method returns the value of a key in the registry
            /// The parameter is a string formatted like:
            ///     
            /// </summary>
            /// <param name="keySeaarchValue"></param>
            /// <returns></returns>
           public string GetKeyValue(string keySeaarchValue)
           {
               RegistryKey regKey;
               RegistryKey regKeyHive;
    
               try
               {
    
                   if (keySeaarchValue.StartsWith(REGISTRY_PREFIX))
                   {
                       string[] regKeyPathAndKeyArray = keySeaarchValue.Split(COLON_DELIMITER.ToCharArray());
                       string[] regKeyPathArray = regKeyPathAndKeyArray[1].Split(COMMA_DELIMITER.ToCharArray());
                       string regKeyPath = regKeyPathArray[0].ToString();
                       string keyName = regKeyPathArray[1].ToString();
                       if (regKeyPath.StartsWith(System.Enum.GetName(typeof(RegistryHive), RegistryHive.HKLM)))
                       {
                           regKeyHive = Registry.LocalMachine;
                       }
                       else if (regKeyPath.StartsWith(System.Enum.GetName(typeof(RegistryHive), RegistryHive.HKCR)))
                       {
                           regKeyHive = Registry.ClassesRoot;
                       }
                       else if (regKeyPath.StartsWith(System.Enum.GetName(typeof(RegistryHive), RegistryHive.HKCU)))
                       {
                           regKeyHive = Registry.CurrentUser;
                       }
                       else if (regKeyPath.StartsWith(System.Enum.GetName(typeof(RegistryHive), RegistryHive.HKU)))
                       {
                           regKeyHive = Registry.Users;
                       }
                       else if (regKeyPath.StartsWith(System.Enum.GetName(typeof(RegistryHive), RegistryHive.HKCC)))
                       {
                           regKeyHive = Registry.Users;
                       }
                       else
                       {
                           throw new ApplicationException("Unknown Key reference: " + regKeyPath.ToString());
                       }
                       int seperatorPosition = regKeyPath.IndexOf(BACKSLASH_DELIMITER, 0) + 1;
                       regKeyPath = regKeyPath.Substring(seperatorPosition, regKeyPath.Length - seperatorPosition);
                       regKey = regKeyHive.OpenSubKey(regKeyPath);
                       if (regKey == null)
                       {
                           throw new ApplicationException("Error: Key not found");
                       }
                       string returnValue = regKey.GetValue(keyName).ToString();
                       if (string.IsNullOrEmpty(returnValue))
                           return ("");
                       return (regKey.GetValue(keyName).ToString());
                   }
                   else
                       //' return the Config string, registry not specified  
                       throw new ApplicationException("prefix word 'registry' missing");
               }
               catch (Exception ex)
               {
                   throw new ApplicationException("Error: " + ex.Message);
               }
           }
        
        }
    }



    Tuesday, February 12, 2013 9:18 AM

Answers

  • User-166373564 posted

    Hi Eugene

    Can someone advise me on the mechanics/code to store connection strings in the registry?

    You could refer the discussion about storing connection strings in registry, I think you could learn the mechanic of storing connection strings in the registry better,

    Storing Connection Strings in Registry

    hope it helps you,

    Kind regards<o:p></o:p>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2013 5:47 AM

All replies

  • User-166373564 posted

    Hi Eugene

    Can someone advise me on the mechanics/code to store connection strings in the registry?

    You could refer the discussion about storing connection strings in registry, I think you could learn the mechanic of storing connection strings in the registry better,

    Storing Connection Strings in Registry

    hope it helps you,

    Kind regards<o:p></o:p>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2013 5:47 AM
  • User1196771204 posted

    hi there,

    can anyone share the drawback of storing the conn string on the registry?

    This looks like a fantastic idea, however, I am still in doubt of the implications 

    Tuesday, February 19, 2013 2:53 AM