0

In my Windows 7 test machine I have a registry key 'MyApp', which is visible under HKEY_CURRENT_USER\Software.

There is also key called 'S-1-5-21-xxxx-1000' under HKEY_USERS, with subkey 'Software' for the logged on user.

The 'MyApp' key is retrieved by a Windows Service which uses a LocalSystem account.

Issue 1 In theory HKEY_CURRENT_USER should reflect the HKEY_USERS entry. However, 'MyApp' is not visible under 'S-1-5-21-xxxx-1000\Software'. But it gets creepier...

Issue 2 The following C# code returns 'null', because it does not retrieve the key from HKEY_CURRENT_USER, but HKEY_USERS (S-1-5-21-xxxx-1000):

var myAppKey = Registry.CurrentUser.OpenSubKey(@"Software\MyApp");

(I know it is from HKEY_USERS (S-1-5-21-xxxx-1000), because when opening subkey 'Software' it does return a key, but with the subkeys from HKEY_USERS (S-1-5-21-xxxx-1000) in stead of HKEY_CURRENT_USER)

Now I must have overlooked something incredibly stupid, or my Windows 7 test machine is FUBAR.

I did my development on a Windows 10 machine, where the Windows Service is working fine.

Please help!

elvenstone
  • 43
  • 6
  • Does this answer your question? [I added a registry key, but I cannot find it programmatically](https://stackoverflow.com/questions/7363768/i-added-a-registry-key-but-i-cannot-find-it-programmatically) – Charlieface May 19 '23 at 17:04
  • 1
    Probably an issue with 32/64 bit redirection. And why are you using Windows 7 when it's way out of support already? – Charlieface May 19 '23 at 17:05
  • The following may be of interest: https://stackoverflow.com/a/75180956/10024425 – Tu deschizi eu inchid May 19 '23 at 18:44
  • @Charlieface: thanks for the suggestion & please find the solution under this post. As for "why are you using Windows 7": this is because my app and service are part of a legacy system that has older Windows versions as a minimum requirement. – elvenstone May 22 '23 at 07:13

1 Answers1

0

Thanks for the quick replies guys! I figured this one out. Turns out that running the service as a LocalSystem account on Windows 7 is the cause for the behaviour.

In stead of returning the registry keys for HKEY_CURRENT_USER (the logged on user), Registry.CurrentUser will return HKEY_USERS.DEFAULT (again: on Windows 7).

So, as a workaround, on Windows 7 I save/retrieve the settings to/from HKEY_LOCAL_MACHINE and all is good !

elvenstone
  • 43
  • 6
  • Note; the answer in this post confirms that LocalSystem account uses the HKU default key: https://stackoverflow.com/questions/510170/the-difference-between-the-local-system-account-and-the-network-service-acco – elvenstone May 22 '23 at 12:16