I cannot read registry values ​​from a service

In my service, I have the following function to take some values ​​from my registry:

Public Function GetKeyValue(ByVal nKey As String, ByVal sPath As String) As String Dim RegKey As RegistryKey Dim kValue As String = Nothing Dim Pos As String If CheckRegistry(sPath) Then Try RegKey = Registry.CurrentUser.OpenSubKey(sPath) kValue = CStr(RegKey.GetValue(nKey)) Catch ex As Exception StartLogFile(" GetKeyValue " & vbNewLine & "Stack Trace= " & ex.StackTrace, EventLogEntryType.Warning) End Try End If Return kValue End Function 

the same function works fine in Windows form, but if I call from a service, it cannot read the value. Does anyone know what is going on?

+4
source share
3 answers

You do not have to store your data in HKEY_CURRENT_USER , but under HKEY_LOCAL_MACHINE , which makes more sense for Windows-Service.

Also remember that you can also set permissions for registry keys. Check also that when reading.

+3
source

Does your service run as the same user as the Windows Forms application? If not, set it to run as the same user.

You will need to save it as CurrentMachine.

+1
source

You almost certainly read another user's registry settings. The service most likely works as one of the built-in user accounts of the system: SYSTEM, LOCALSERVICE or NETWORKSERVICE. These are not interactive users.

Your design is mostly erroneous, and I suspect that you will need to move these settings to a file that is not part of the user profile.

+1
source

Source: https://habr.com/ru/post/1344480/


All Articles