In C# the following creates a permanent environment variable:
Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.Machine);