views:

40

answers:

1

In a VB.NET application that runs as a service, I am getting a value of "Nothing" returned when I use Registry.CurrentUser.OpenSubKey(). The same code executes correctly when the application runs in desktop mode. I am not much on VB.NET and unfortunately, this is not my code.

Code:

Dim regURL As String = "Software\MyCompany\" + _
                  System.Reflection.Assembly.GetCallingAssembly.GetName.Name + _
                  "\Settings"

regKey = My.Computer.Registry.CurrentUser.OpenSubKey(regURL, True)

The regKey is Nothing when used by a service. Desktop returns a valid registry key object to which I feel verifies the URL. Any ideas I am using CurrentUser because this code does not work with LocalMachine when operating on Windows Server 2008.

Thanks in advance,

Craig

+1  A: 

For this to be a valid test you have to ensure that your service is configured at installation time to run as the same account that you used for desktop testing. Are they the same?

If not, the registry values will be different since CurrentUser is an alias for user-specific information - not the same for each user.

If they don't match, you should be able to retest by altering the service settings via AdminstrativeTools -> Services - right click your service and select Properties, then alter the service account thru the Log On tab.

Steve Townsend
They are the same. I even tried change the logon settings of the service and it did not affect the return value.
cbuck12000
@cbuck12000 - this exchange (http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/f75f319c-acb4-422d-a247-ad8378633fd3) suggests that this won't work for you. Why can you not use LocalService or NetworkService? It's unusual for a service to need to run as a specific desktop user.
Steve Townsend