Reputation: 1061
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