Accessing registry values from a windows service

  • Thread starter Thread starter Stu
  • Start date Start date
S

Stu

Hi,

I am storing various settings for a windows service in the registry - but
for some reason the windows service will not read them.

Namespaces:
Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics

The class inherits:
Inherits System.ServiceProcess.ServiceBase

My code is:
GetSetting("MyProject", "Settings", "LogfilePath")

The function I am using works fine from within a window forms app.
Anyine any ideas?

Thanks in advance,

Stu
 
I use the following in my services:


Imports Microsoft.Win32
------
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\myproduct\options", False)

If regKey Is Nothing Then
'not setup yet
Else
'retrieve value as follows - regKey.GetValue("UserName")
End If

User submitted from AEWNET (http://www.aewnet.com/)
 
But you will also need to define registry permissions for read/writing the
registry to be on the safe side

The other user's example is what I would do too, except that I would use the
following method:

Imports Microsoft.Win32
------
Dim regKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("SOFTWARE\myproduct\options", False)

If Not (regKey Is Nothing) Then

Dim strSetting As String = CType(regKey.GetValue("UserName"), String)

End If

regKey.Close ' <--- Don't forget to close the registry key

Then you can use 'strSetting', which will make your life easier
 
Back
Top