+#if !NET_2_1
+ [MethodImplAttribute (MethodImplOptions.InternalCall)]
+ private static extern void internalBroadcastSettingChange ();
+
+ 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");
+ }
+ }
+
+ public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
+ {
+ IDictionary variables = (IDictionary)new Hashtable ();
+ switch (target) {
+ case EnvironmentVariableTarget.Process:
+ variables = GetEnvironmentVariables ();
+ break;
+ case EnvironmentVariableTarget.Machine:
+ new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
+ if (IsRunningOnWindows) {
+ using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
+ string[] value_names = env.GetValueNames ();
+ foreach (string value_name in value_names)
+ variables.Add (value_name, env.GetValue (value_name));
+ }
+ }
+ break;
+ case EnvironmentVariableTarget.User:
+ new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
+ if (IsRunningOnWindows) {
+ using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment")) {
+ string[] value_names = env.GetValueNames ();
+ foreach (string value_name in value_names)
+ variables.Add (value_name, env.GetValue (value_name));
+ }
+ }
+ break;
+ default:
+ throw new ArgumentException ("target");
+ }
+ return variables;
+ }
+
+ [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
+ public static void SetEnvironmentVariable (string variable, string value)
+ {
+ SetEnvironmentVariable (variable, value, EnvironmentVariableTarget.Process);
+ }
+
+ [EnvironmentPermission (SecurityAction.Demand, Unrestricted = true)]
+ public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
+ {
+ if (variable == null)
+ throw new ArgumentNullException ("variable");
+ if (variable == String.Empty)
+ throw new ArgumentException ("String cannot be of zero length.", "variable");
+ if (variable.IndexOf ('=') != -1)
+ throw new ArgumentException ("Environment variable name cannot contain an equal character.", "variable");
+ if (variable[0] == '\0')
+ throw new ArgumentException ("The first char in the string is the null character.", "variable");
+
+ switch (target) {
+ case EnvironmentVariableTarget.Process:
+ InternalSetEnvironmentVariable (variable, value);
+ break;
+ case EnvironmentVariableTarget.Machine:
+ if (!IsRunningOnWindows)
+ return;
+ using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) {
+ if (String.IsNullOrEmpty (value))
+ env.DeleteValue (variable, false);
+ else
+ env.SetValue (variable, value);
+ internalBroadcastSettingChange ();
+ }
+ break;
+ case EnvironmentVariableTarget.User:
+ if (!IsRunningOnWindows)
+ return;
+ using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", true)) {
+ if (String.IsNullOrEmpty (value))
+ env.DeleteValue (variable, false);
+ else
+ env.SetValue (variable, value);
+ internalBroadcastSettingChange ();
+ }
+ break;
+ default:
+ throw new ArgumentException ("target");
+ }
+ }
+
+ [MethodImplAttribute (MethodImplOptions.InternalCall)]
+ internal static extern void InternalSetEnvironmentVariable (string variable, string value);
+#endif
+ [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
+ public static void FailFast (string message)
+ {
+ throw new NotImplementedException ();
+ }
+
+#if NET_4_0 || MOONLIGHT
+ [SecurityCritical]
+ public static void FailFast (string message, Exception exception)
+ {
+ throw new NotImplementedException ();
+ }
+#endif
+
+#if NET_4_0
+ public static bool Is64BitOperatingSystem {
+ get { return IntPtr.Size == 8; } // FIXME: is this good enough?
+ }
+
+ public static bool Is64BitProcess {
+ get { return Is64BitOperatingSystem; }
+ }
+
+ public static int SystemPageSize {
+ get { return GetPageSize (); }
+ }
+#endif
+
+ public static extern int ProcessorCount {
+ [EnvironmentPermission (SecurityAction.Demand, Read="NUMBER_OF_PROCESSORS")]
+ [MethodImplAttribute (MethodImplOptions.InternalCall)]
+ get;
+ }