Faced a problem. There is a WF, it has a part of the code for storing values ​​in the registry:

using (RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"Software\Test")) { rk.SetValue("language", path); } 

Similarly, I read the value, at this stage everything is good:

  using (RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"Software\Test")) { string path = rk.GetValue("language", "").ToString(); } 

But I try to read this value from the SERVICE written by me - nothing is present, nothing is read. Tell me, are there any nuances when reading the registry from the service? Where to look for a mistake and what is it?

  • one
    Your code works well for me, once again explain what is wrong? - Lolidze
  • I write the value to the registry from WF, and I read it in the service. With this option, the service does not read anything. - Alexander Puzanov
  • one
    @AlexanderPuzanov service, meaning, OS service? Does it run under the same user? - default locale
  • Yes, OS service. Do you have a version with WF and OS service? - Alexander Puzanov
  • 2
    I have not tried it, but obviously, to access the same Registry.CurrentUser and the application and service should run under one user, most likely you don’t. PS WF abbreviation is confusing, write Windows Forms or just an application. - default locale

1 answer 1

Registry.CurrentUser returns the current user branch. If the service is running under the system account, the branch corresponding to the System user will be returned.

Depending on the situation, you can try:

  1. run the application and service from a single user;
  2. create a key in Registry.LocalMachine - additional permissions may be required;
  3. create a key for the user, and in the service to sort through the branches of users through the Registry.Users - there will be problems when the user and the service work in parallel;
  4. exchange data not through the registry (file system, database).
  • 1 does not fit, because the service can be started under any user with the rights. 2 Not all users can have access to data from branch 3. When the service starts, the settings panel in the program is blocked, so there will be no conflict. Only reading is performed from the service. . But users with different rights and access issue. 4 With the registry, the option definitely disappears, I see only the option to use the config file in CommonAppData - Alexander Puzanov
  • 2
    @AlexanderPuzanov I see. The approach with a public location seems the easiest. In a similar question in English, we consider the option of overriding the location of settings for an application, which is somewhat more complicated. Experiment, select the appropriate option. - default locale