Mehdi Ben Hamida
Mehdi Ben Hamida

Reputation: 1061

Set a new environement variable: Requested registry access is not allowed

I'am trying to write a code in C# that set an environment variable from two lists of variables name and their supposed content. This how I am doing:

var VariableName = new[] {"ROOT"};
var VariableContent = new[] { "C:\\" };
var Variables = VariableName.Zip(VariableContent, (n, c) => new { Name = n, Content = c });
foreach (var Variable in Variables)
{
    if (Environment.GetEnvironmentVariable(Variable.Name) == null)
    {        
        Environment.SetEnvironmentVariable(Variable.Name, Variable.Content, EnvironmentVariableTarget.Machine);   

    }

the problem is that SetEnvironmentVariable method is rturning the following exception:

System.Security.SecurityException: Requested registry access is not allowed.

How can this be solved ?

Upvotes: 1

Views: 2625

Answers (1)

Ehsan Zargar Ershadi
Ehsan Zargar Ershadi

Reputation: 24853

Run your application in administrator mode

Upvotes: 4

Related Questions