* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / System / Environment.cs
index b872df68c2781f78531888b197d01e5b13ed6323..34aab93f79f970d64e7f3eb654401b2a0f31a7f9 100644 (file)
@@ -59,8 +59,8 @@ namespace System {
                 * Changes which are already detected at runtime, like the addition
                 * of icalls, do not require an increment.
                 */
-               private const int mono_corlib_version = 41;
-               
+               private const int mono_corlib_version = 54;
+               
                [MonoTODO]
                public enum SpecialFolder
                {       // TODO: Determine if these windoze style folder identifiers 
@@ -197,7 +197,7 @@ namespace System {
                public static string StackTrace {
                        [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
                        get {
-                               System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (1);
+                               System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (1, true);
                                return trace.ToString ();
                        }
                }
@@ -254,15 +254,7 @@ namespace System {
                /// </summary>
                public static Version Version {
                        get {
-#if NET_2_0
-                               // FIXME: this is the version number for MS.NET 2.0 beta1. 
-                               // It must be changed when the final version is released.
-                               return new Version (2, 0, 50215, 16);
-#elif NET_1_1                              
-                               return new Version (1, 1, 4322, 573);
-#else
-                               return new Version (1, 0, 3705, 288);
-#endif
+                               return new Version (Consts.FxFileVersion);
                        }
                }
 
@@ -522,7 +514,6 @@ namespace System {
 
                 
 #if NET_2_0
-               [MonoTODO ("Machine and User targets aren't supported")]
                public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
                {
                        switch (target) {
@@ -530,67 +521,105 @@ namespace System {
                                return GetEnvironmentVariable (variable);
                        case EnvironmentVariableTarget.Machine:
                                new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
-                               // under Windows this reads the LOCAL_MACHINE registry key for env vars
-                               throw new NotImplementedException ();
+                               if (!IsRunningOnWindows)
+                                       return null;
+                               using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
+                                       return env.GetValue (variable).ToString ();
+                               }
                        case EnvironmentVariableTarget.User:
                                new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
-                               // under Windows this reads the CURRENT_USER registry key for env vars
-                               throw new NotImplementedException ();
+                               if (!IsRunningOnWindows)
+                                       return null;
+                               using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", false)) {
+                                       return env.GetValue (variable).ToString ();
+                               }
                        default:
                                throw new ArgumentException ("target");
                        }
                }
 
-               [MonoTODO ("Machine and User targets aren't supported")]
                public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
                {
+                       IDictionary variables = (IDictionary)new Hashtable ();
                        switch (target) {
                        case EnvironmentVariableTarget.Process:
-                               return GetEnvironmentVariables ();
+                               variables = GetEnvironmentVariables ();
+                               break;
                        case EnvironmentVariableTarget.Machine:
                                new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
-                               // under Windows this reads the LOCAL_MACHINE registry key for env vars
-                               throw new NotImplementedException ();
+                               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 ();
-                               // under Windows this reads the CURRENT_USER registry key for env vars
-                               throw new NotImplementedException ();
+                               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;
                }
 
-               [MonoTODO]
                [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
                public static void SetEnvironmentVariable (string variable, string value)
                {
-                       InternalSetEnvironmentVariable (variable, value);
+                       SetEnvironmentVariable (variable, value, EnvironmentVariableTarget.Process);
                }
 
-               [MonoTODO]
-               [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
+               [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:
-                               // under Windows this reads the LOCAL_MACHINE registry key for env vars
-                               throw new NotImplementedException ();
+                               if (!IsRunningOnWindows)
+                                       return;
+                               using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) {
+                                       if (value == null || value.Length == 0)
+                                               env.DeleteValue (variable, false);
+                                       else
+                                               env.SetValue (variable, value);
+                               }
+                               break;
                        case EnvironmentVariableTarget.User:
-                               // under Windows this reads the CURRENT_USER registry key for env vars
-                               throw new NotImplementedException ();
+                               if (!IsRunningOnWindows)
+                                       return;
+                               using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", true)) {
+                                       if (value == null || value.Length == 0)
+                                               env.DeleteValue (variable, false);
+                                       else
+                                               env.SetValue (variable, value);
+                               }
+                               break;
                        default:
                                throw new ArgumentException ("target");
                        }
                }
 
-               // FIXME: to be changed as an icall when implemented
-               internal static void InternalSetEnvironmentVariable (string variable, string value)
-               {
-                       throw new NotImplementedException ();
-               }
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal static extern void InternalSetEnvironmentVariable (string variable, string value);
 
                [MonoTODO]
                public static int ProcessorCount {
@@ -608,8 +637,8 @@ namespace System {
                {
                        throw new NotImplementedException ();
                }
-#endif                
-                
+#endif
+
                // private methods
 
                internal static bool IsRunningOnWindows {