Accessing registry values from a windows service

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
 
G

Guest

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/)
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top