Search code examples
c#registry

How to set environment variable to machine in c#


I want's to store sql server name to system Environment variables and get when it needed;

i tried this:

Environment.SetEnvironmentVariable("Varname","VarVal",EnvironmnetVariableTarget.Machine);

But it shows an error:

Requested Registry Access Not Allowed

Called Security Exception....

What to do?


Solution

  • You're accessing HKEY_LOCAL_MACHINE. You will need to get the required access from Windows. Take a look at this post about getting the right privilege.