title: C# set environment variable
body: <p>I have problem with setting environment variables using C#.</p>
  
  <p>I need to modify some environment variables on some circumstances. For example I need to  modify NDSRC variable.</p>
  
  <p>I use:</p>
  
  <pre><code>Environment.SetEnvironmentVariable("MY_VARIABLE", "value", EnvironmentVariableTarget.Machine);
  </code></pre>
  
  <p>This works fine.</p>
  
  <p>Next i run some script whitch uses the variable. And now there is a problem, because script does not see the variable.</p>
  
  <p>Example:
  Set Path variable (add a directory at the end) using </p>
  
  <pre><code>string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) + ";c:\\";
  Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Machine);
  </code></pre>
  
  <p>Open windows command line (Start->run->cmd.exe).</p>
  
  <p>In command line type cmd</p>
  
  <p>The system can not find cmd.exe: 'cmd' is not recognized as an internal or external command, operable program or batch file.</p>
  
  <p>If you check Windows settings - > Environment Variables, Path is correctly set to new value. If you check in opened command prompt, it is also set.</p>