Lefteris Gkinis Lefteris Gkinis - 4 months ago 19
Vb.net Question

I Can't Read registry values from a service

Within my service i have the following function in order 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 within a Windows form, but if i call from a service then she can't read the value.
Is there anybody how knows what is going on?

Answer

You should not store your data in HKEY_CURRENT_USER but under HKEY_LOCAL_MACHINE that makes more sense for a Windows-Service.

Be also aware that you can also set Permissions on Registry Keys. Check also that when try reading.

Comments