Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a method which returns the value of a windows registry key. It is working fine in a windows service in one machine (windows server 2008 R2 standard).

Please have a look at below.

C#
private string GetLastDate()
{
  Microsoft.Win32.RegistryKey key;
  key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("EMPSWIPELASTDATE");
  String RegLstDate = (String)key.GetValue("LASTDATE");
  return RegLstDate;
}


issue:
The issue is, when i install the same service in another windows machine (same configuration), service is running but it returns NULL.

Kindly list the possible reasons.

Thanks,
Posted
Comments
Bernhard Hiller 7-Aug-13 9:46am    
Registry.CurrentUser belongs to the account the service is using - typically LocalSystem. For trouble shooting, try running your service with a different account (e.g. yours) and try again.
NPSSR 7-Aug-13 10:27am    
Yes. Got it. Thanks.

1 solution

The service is running as a user that does not have that registry key set on the other machine.
 
Share this answer
 
Comments
NPSSR 7-Aug-13 9:39am    
Keys are available with values.
Fredrik Bornander 7-Aug-13 9:47am    
I don't think they are. I think you're looking under the wrong user.
According to the documentation of the GetValue method;
Retrieves the value associated with the specified name. Returns null if the name/value pair does not exist in the registry.

If you log on to the machine and check the registry under Current user you get the user you're logged in as, are you 100% sure that's the same user as the one running the service (not starting the service, mind you)?
NPSSR 7-Aug-13 10:26am    
yes. It should be as user id whereas it was in LocalSystem. Got it. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900