moakley
moakley

Reputation: 21

Read HKLM from windows service on windows 7

What I need to do is to read an application specific string value from HKLM from a windows service. The registry hive and values were added using a windows form, a tool for modifying configuration values for the windows service. I am not able to read the values from my windows service, "Requested registry access is not allowed". I am trying to open and read as follows:

rk = Registry.LocalMachine.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree);
if (rk != null)
{
   value = rk.GetValue(setting).ToString();
}

Upvotes: 1

Views: 3673

Answers (2)

Melloware
Melloware

Reputation: 12029

I do this from a Windows .NET Service right now with this code.

   public const string REG_KEY_MINE = @"SOFTWARE\Mine\Test";

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_KEY_MINE, false))
{

    UDP_PORT = (int) key.GetValue("UdpPort", 43221);
    TCP_PORT = (int) key.GetValue("TcpPort", 8005);
}

So it is possible you are running your service not under the default Administrator privileges???

Upvotes: 2

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

Are the app and service running under different credentials? The configuration app may be having registry virtualization applied to it (http://msdn.microsoft.com/en-us/library/aa965884.aspx).

OR there could be a difference between the bitness of the config app and the service and are seeing different views of the registry (http://msdn.microsoft.com/en-us/library/aa384232.aspx).

Upvotes: 2

Related Questions