Registry.CurrentUser.OpenSubKey returns Nothing when called from a Service
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 = 开发者_StackOverflow社区"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
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.
精彩评论