0

I have a Windows Service which runs under a domain account (which is not an Administrator).

I am starting a Thread in the Windows Service, The thread internally creates a Task. Now the Task tries to setup few Environment Variables at Process Level using,

Environment.SetEnvironmentVariable("var_name","value");

That's when I get below Error:

    System.Security.SecurityException: Requested registry access is not allowed. 
    at Microsoft.Win32.RegistryKey.OpenSubKey(string name, Boolean writable)
    at System.Environment.SetEnvironmentVariable(String variable, String value, EnvironmentVariableTarget target)
....... followed by the code Stack Trace.

Would anyone be able to help please?

Note: I can neither set the environment at Machine level due to constraint from Risk team nor at User Level as the user is used by multiple application and variable value might clash(so not suggested).

  • Possible duplicate of [Requested registry access is not allowed](http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed) – Sinatr Apr 03 '17 at 15:00
  • @Sinatr I am not editing registry as I am setting up the Environment Variable at Process level.. The question in which I am interested is whether the Process/Thread/Task scenario I explained above makes a difference in setting up the Environment Variable; as it is being setup from the Task. – Mayank Vashishtha Apr 03 '17 at 15:04
  • Hint: look at the call stack, which method is called there and what it does? – Sinatr Apr 03 '17 at 15:06
  • @Sinatr It's already working in the lower environment... but fails in UAT, which suggests that it might be related with access right of the user under which it runs. – Mayank Vashishtha Apr 03 '17 at 15:09
  • @Sinatr, solved the issue, however I don't know why but had to set the environment variable like Environment.SetEnvironmentVariable("var_name","value", EnvironmentVariableTarget.Process); – Mayank Vashishtha Apr 03 '17 at 17:47

0 Answers0