public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
{
switch (target) {
case EnvironmentVariableTarget.Process:
return GetEnvironmentVariable (variable);
case EnvironmentVariableTarget.Machine:
new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
if (!IsRunningOnWindows)
return null;
using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
object regvalue = env.GetValue (variable);
return (regvalue == null) ? null : regvalue.ToString ();
}
case EnvironmentVariableTarget.User:
new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
if (!IsRunningOnWindows)
return null;
using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", false)) {
object regvalue = env.GetValue (variable);
return (regvalue == null) ? null : regvalue.ToString ();
}
default:
throw new ArgumentException ("target");
}
}