Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>SOLUTION:</strong> Marco's answer was great and technically answered my question - except that I found out that the environment variables SYSTEMROOT and SYSTEMDRIVE are not really set in the registry where all environment variables are set, so, the chosen answer works for all variables except those two, which I specified in the OP.</p> <p>SYSTEMROOT is defined on the registry in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRoot, and apparently (after more research), SYSTEMDRIVE is generated as a substring of SYSTEMDRIVE.</p> <p>So, to get SYSTEMDRIVE and SYSTEMROOT from registry and load them into the environment:</p> <pre><code>using Microsoft.Win32; namespace MySpace { public class Setup { public Setup() { SetUpEnvironment(); } private void SetUpEnvironment() { string test_a = Environment.GetEnvironmentVariable("SYSTEMDRIVE", EnvironmentVariableTarget.Process); string test_b = Environment.GetEnvironmentVariable("SYSTEMROOT", EnvironmentVariableTarget.Process); if (test_a == null || test_a.Length == 0 || test_b == null || test_b.Length == 0) { string RegistryPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"; string SYSTEMROOT = (string) Registry.GetValue(RegistryPath, "SystemRoot", null); if (SYSTEMROOT == null) { throw new System.ApplicationException("Cannot access registry key " + RegistryPath); } string SYSTEMDRIVE = SYSTEMROOT.Substring(0, SYSTEMROOT.IndexOf(':') + 1); Environment.SetEnvironmentVariable("SYSTEMROOT", SYSTEMROOT, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("SYSTEMDRIVE", SYSTEMDRIVE, EnvironmentVariableTarget.Process); } } } } </code></pre> <p>Then you can just call <code>Setup setup = new Setup();</code> from other classes. And that's it. :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload