Search code examples
c#iisregistry

Reading HKEY_CURRENT_USER under IIS


I am trying to read the registry path

HKEY_CURRENT_USER\Software\MyFolder

By running the following code

RegistryKey key1 = Registry.CurrentUser.OpenSubKey(path);
if (key1 != null)
{
    RegistryKey key2 = key1.OpenSubKey(subPath);
    if (key2 != null)
    {
        return key2.GetValue(registryKey);
    }
}

Where path = Software and subPath = MyFolder

  1. key2 is always NULL
  2. I believe this code is actually reading from HKEY_LOCAL_MACHINE\Software\MyFolder because it will default to this under the IIS account (I am running under application pool identity)

How to I force this code to access HKEY_CURRENT_USER? Do I need to change the identity of the app pool? Or some other way?


Solution

  • This is a web app - I would stay far away from the registry. Put the information in a configuration file or a database. Others have explained why you can't open it. Here is a little more information around it.

    Link for information below.

    • HKEY_LOCAL_MACHINE (HKLM): Contains configuration information related to all hardware devices and software programs that are installed on your system.

    • HKEY_USERS (HKU): Contains information related to all user profiles configured on the system. HKEY_USERS key has a template, which is used by your system to generate new user profiles with default configuration.

    • HKEY_CLASSES_ROOT (HKCR): Stores information about file types and extensions, protocols, and classes registered on your computer. This key is user-specific and extracts user-specific data from the HKLM key. The information displayed in the HKCR subtree is obtained from the HKLMSOFTWAREClasses and HKCUSOFTWAREClasses keys.

    • HKEY_CURRENT_USER (HKCU): HKEY_CURRENT_USER key contains information related to desktop settings, variables, variables, environment user folders, and other user-specific settings for the current user. Like HKCR, this key extracts user-specific information from the HKU key. The information displayed by this key is pulled out from the HKUSecurity ID key of the user logged in on the system.

    • HKEY_CURRENT_CONFIG (HKCC): This key contains information about hardware configuration for the current user. This key extracts relevant information from the HKLMSYSTEM CurrentControlSet CurrentControlSetHardware Profiles key.

    If you do decide to still use the registry - store the information in HKLM.