New tests.
[mono.git] / mcs / class / corlib / System / Environment.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.Environment.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 //                 Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created:        Saturday, August 11, 2001 
10 //
11 //------------------------------------------------------------------------------
12 //
13 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.IO;
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Security;
39 using System.Security.Permissions;
40 using System.Text;
41 using System.Runtime.InteropServices;
42
43 namespace System {
44
45 #if NET_2_0
46         [ComVisible (true)]
47 #endif
48 #if NET_2_0
49         public static class Environment {
50 #else
51         public sealed class Environment {
52
53                 private Environment ()
54                 {
55                 }
56 #endif
57                 /*
58                  * This is the version number of the corlib-runtime interface. When
59                  * making changes to this interface (by changing the layout
60                  * of classes the runtime knows about, changing icall signature or
61                  * semantics etc), increment this variable. Also increment the
62                  * pair of this variable in the runtime in metadata/appdomain.c.
63                  * Changes which are already detected at runtime, like the addition
64                  * of icalls, do not require an increment.
65                  */
66                 private const int mono_corlib_version = 69;
67
68 #if NET_2_0
69                 [ComVisible (true)]
70 #endif
71                 public enum SpecialFolder
72                 {       // TODO: Determine if these windoze style folder identifiers 
73                         //       have unix/linux counterparts
74 #if NET_2_0
75                         MyDocuments = 0x05,
76 #endif
77 #if NET_1_1
78                         Desktop = 0x00,
79                         MyComputer = 0x11,
80 #endif
81                         Programs = 0x02,
82                         Personal = 0x05,
83                         Favorites = 0x06,
84                         Startup = 0x07,
85                         Recent = 0x08,
86                         SendTo = 0x09,
87                         StartMenu = 0x0b,
88                         MyMusic = 0x0d,
89                         DesktopDirectory = 0x10,
90                         Templates = 0x15,
91                         ApplicationData = 0x1a,
92                         LocalApplicationData = 0x1c,
93                         InternetCache = 0x20,
94                         Cookies = 0x21,
95                         History = 0x22,
96                         CommonApplicationData   = 0x23,
97                         System = 0x25,
98                         ProgramFiles = 0x26,
99                         MyPictures = 0x27,
100                         CommonProgramFiles = 0x2b,
101                 }
102
103                 /// <summary>
104                 /// Gets the command line for this process
105                 /// </summary>
106                 public static string CommandLine {
107                         // note: security demand inherited from calling GetCommandLineArgs
108                         get {
109                                 // FIXME: we may need to quote, but any sane person
110                                 // should use GetCommandLineArgs () instead.
111                                 return String.Join (" ", GetCommandLineArgs ());
112                         }
113                 }
114
115                 /// <summary>
116                 /// Gets or sets the current directory. Actually this is supposed to get
117                 /// and/or set the process start directory acording to the documentation
118                 /// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
119                 /// </summary>
120                 public static string CurrentDirectory
121                 {
122                         get {
123                                 return Directory.GetCurrentDirectory ();
124                         }
125                         set {
126                                 Directory.SetCurrentDirectory (value);
127                         }
128                 }
129
130                 /// <summary>
131                 /// Gets or sets the exit code of this process
132                 /// </summary>
133                 public extern static int ExitCode
134                 {       
135                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
136                         get;
137                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
138                         set;
139                 }
140
141 #if NET_1_1
142                 static
143 #endif
144                 public extern bool HasShutdownStarted
145                 {
146                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
147                         get;
148                 }
149                 
150
151                 /// <summary>
152                 /// Gets the name of the local computer
153                 /// </summary>
154                 public extern static string MachineName {
155                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
156                         [EnvironmentPermission (SecurityAction.Demand, Read="COMPUTERNAME")]
157                         [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
158                         get;
159                 }
160
161                 /// <summary>
162                 /// Gets the standard new line value
163                 /// </summary>
164                 public extern static string NewLine {
165                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
166                         get;
167                 }
168
169                 //
170                 // Support methods and fields for OSVersion property
171                 //
172                 static OperatingSystem os;
173
174                 internal static extern PlatformID Platform {
175                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
176                         get;
177                 }
178
179                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180                 internal static extern string GetOSVersionString ();
181
182                 /// <summary>
183                 /// Gets the current OS version information
184                 /// </summary>
185                 public static OperatingSystem OSVersion {
186                         get {
187                                 if (os == null) {
188                                         Version v = Version.CreateFromString (GetOSVersionString ());
189                                         PlatformID p = Platform;
190 #if NET_2_0
191                                         if ((int) p == 128)
192                                                 p = PlatformID.Unix;
193 #endif
194                                         os = new OperatingSystem (p, v);
195                                 }
196                                 return os;
197                         }
198                 }
199
200                 /// <summary>
201                 /// Get StackTrace
202                 /// </summary>
203                 public static string StackTrace {
204                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
205                         get {
206                                 System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (0, true);
207                                 return trace.ToString ();
208                         }
209                 }
210
211                 /// <summary>
212                 /// Get a fully qualified path to the system directory
213                 /// </summary>
214                 public static string SystemDirectory {
215                         get {
216                                 return GetFolderPath (SpecialFolder.System);
217                         }
218                 }
219
220                 /// <summary>
221                 /// Get the number of milliseconds that have elapsed since the system was booted
222                 /// </summary>
223                 public extern static int TickCount {
224                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
225                         get;
226                 }
227
228                 /// <summary>
229                 /// Get UserDomainName
230                 /// </summary>
231                 public static string UserDomainName {
232                         // FIXME: this variable doesn't exist (at least not on WinXP) - reported to MS as FDBK20562
233                         [EnvironmentPermission (SecurityAction.Demand, Read="USERDOMAINNAME")]
234                         get {
235                                 return MachineName;
236                         }
237                 }
238
239                 /// <summary>
240                 /// Gets a flag indicating whether the process is in interactive mode
241                 /// </summary>
242                 [MonoTODO ("Currently always returns false, regardless of interactive state")]
243                 public static bool UserInteractive {
244                         get {
245                                 return false;
246                         }
247                 }
248
249                 /// <summary>
250                 /// Get the user name of current process is running under
251                 /// </summary>
252                 public extern static string UserName {
253                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
254                         [EnvironmentPermission (SecurityAction.Demand, Read="USERNAME;USER")]
255                         get;
256                 }
257
258                 /// <summary>
259                 /// Get the version of the common language runtime 
260                 /// </summary>
261                 public static Version Version {
262                         get {
263                                 return new Version (Consts.FxFileVersion);
264                         }
265                 }
266
267                 /// <summary>
268                 /// Get the amount of physical memory mapped to process
269                 /// </summary>
270                 [MonoTODO ("Currently always returns zero")]
271                 public static long WorkingSet {
272                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
273                         get { return 0; }
274                 }
275
276                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
277                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
278                 public extern static void Exit (int exitCode);
279
280                 /// <summary>
281                 /// Substitute environment variables in the argument "name"
282                 /// </summary>
283                 public static string ExpandEnvironmentVariables (string name)
284                 {
285                         if (name == null)
286                                 throw new ArgumentNullException ("name");
287
288                         int off1 = name.IndexOf ('%');
289                         if (off1 == -1)
290                                 return name;
291
292                         int len = name.Length;
293                         int off2 = 0;
294                         if (off1 == len - 1 || (off2 = name.IndexOf ('%', off1 + 1)) == -1)
295                                 return name;
296
297                         StringBuilder result = new StringBuilder ();
298                         result.Append (name, 0, off1);
299                         Hashtable tbl = null;
300                         do {
301                                 string var = name.Substring (off1 + 1, off2 - off1 - 1);
302                                 string value = GetEnvironmentVariable (var);
303                                 if (value == null && Environment.IsRunningOnWindows) {
304                                         // On windows, env. vars. are case insensitive
305                                         if (tbl == null)
306                                                 tbl = GetEnvironmentVariablesNoCase ();
307
308                                         value = tbl [var] as string;
309                                 }
310                                 
311                                 // If value not found, add %FOO to stream,
312                                 //  and use the closing % for the next iteration.
313                                 // If value found, expand it in place of %FOO%
314                                 if (value == null) {
315                                         result.Append ('%');
316                                         result.Append (var);
317                                         off2--;
318                                 } else {
319                                         result.Append (value);
320                                 }
321                                 int oldOff2 = off2;
322                                 off1 = name.IndexOf ('%', off2 + 1);
323                                 // If no % found for off1, don't look for one for off2
324                                 off2 = (off1 == -1 || off2 > len-1)? -1 :name.IndexOf ('%', off1 + 1);
325                                 // textLen is the length of text between the closing % of current iteration
326                                 //  and the starting % of the next iteration if any. This text is added to output
327                                 int textLen;
328                                 // If no new % found, use all the remaining text
329                                 if (off1 == -1 || off2 == -1)
330                                         textLen = len - oldOff2 - 1;
331                                 // If value found in current iteration, use text after current closing % and next %
332                                 else if(value != null)
333                                         textLen = off1 - oldOff2 - 1;
334                                 // If value not found in current iteration, but a % was found for next iteration,
335                                 //  use text from current closing % to the next %.
336                                 else
337                                         textLen = off1 - oldOff2;
338                                 if(off1 >= oldOff2 || off1 == -1)
339                                         result.Append (name, oldOff2+1, textLen);
340                         } while (off2 > -1 && off2 < len);
341                                 
342                         return result.ToString ();
343
344                 }
345
346                 /// <summary>
347                 /// Return an array of the command line arguments of the current process
348                 /// </summary>
349                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
350                 [EnvironmentPermissionAttribute (SecurityAction.Demand, Read = "PATH")]
351                 public extern static string[] GetCommandLineArgs ();
352
353                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
354                 internal extern static string internalGetEnvironmentVariable (string variable);
355
356                 /// <summary>
357                 /// Return a string containing the value of the environment
358                 /// variable identifed by parameter "variable"
359                 /// </summary>
360                 public static string GetEnvironmentVariable (string variable)
361                 {
362                         if (SecurityManager.SecurityEnabled) {
363                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, variable).Demand ();
364                         }
365                         return internalGetEnvironmentVariable (variable);
366                 }
367
368                 static Hashtable GetEnvironmentVariablesNoCase ()
369                 {
370                         Hashtable vars = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
371                                                         CaseInsensitiveComparer.Default);
372
373                         foreach (string name in GetEnvironmentVariableNames ()) {
374                                 vars [name] = internalGetEnvironmentVariable (name);
375                         }
376
377                         return vars;
378                 }
379
380                 /// <summary>
381                 /// Return a set of all environment variables and their values
382                 /// </summary>
383 #if NET_2_0
384                 public static IDictionary GetEnvironmentVariables ()
385                 {
386                         StringBuilder sb = null;
387                         if (SecurityManager.SecurityEnabled) {
388                                 // we must have access to each variable to get the lot
389                                 sb = new StringBuilder ();
390                                 // but (performance-wise) we do not want a stack-walk
391                                 // for each of them so we concatenate them
392                         }
393
394                         Hashtable vars = new Hashtable ();
395                         foreach (string name in GetEnvironmentVariableNames ()) {
396                                 vars [name] = internalGetEnvironmentVariable (name);
397                                 if (sb != null) {
398                                         sb.Append (name);
399                                         sb.Append (";");
400                                 }
401                         }
402
403                         if (sb != null) {
404                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, sb.ToString ()).Demand ();
405                         }
406                         return vars;
407                 }
408 #else
409                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
410                 public static IDictionary GetEnvironmentVariables ()
411                 {
412                         Hashtable vars = new Hashtable ();
413                         foreach (string name in GetEnvironmentVariableNames ()) {
414                                 vars [name] = internalGetEnvironmentVariable (name);
415                         }
416                         return vars;
417                 }
418 #endif
419
420                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
421                 private extern static string GetWindowsFolderPath (int folder);
422
423                 /// <summary>
424                 /// Returns the fully qualified path of the
425                 /// folder specified by the "folder" parameter
426                 /// </summary>
427                 public static string GetFolderPath (SpecialFolder folder)
428                 {
429                         string dir = null;
430
431                         if (Environment.IsRunningOnWindows) {
432                                 dir = GetWindowsFolderPath ((int) folder);
433                         } else {
434                                 dir = InternalGetFolderPath (folder);
435                         }
436
437                         if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
438                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
439                         }
440                         return dir;
441                 }
442
443                 private static string ReadXdgUserDir (string config_dir, string home_dir, 
444                         string key, string fallback)
445                 {
446                         string env_path = internalGetEnvironmentVariable (key);
447                         if (env_path != null && env_path != String.Empty) {
448                                 return env_path;
449                         }
450
451                         string user_dirs_path = Path.Combine (config_dir, "user-dirs.dirs");
452
453                         if (!File.Exists (user_dirs_path)) {
454                                 return Path.Combine (home_dir, fallback);
455                         }
456
457                         try {
458                                 using(StreamReader reader = new StreamReader (user_dirs_path)) {
459                                         string line;
460                                         while ((line = reader.ReadLine ()) != null) {
461                                                 line = line.Trim ();
462                                                 int delim_index = line.IndexOf ('=');
463                         if(delim_index > 8 && line.Substring (0, delim_index) == key) {
464                             string path = line.Substring (delim_index + 1).Trim ('"');
465                             bool relative = false;
466
467                             if (path.StartsWith ("$HOME/")) {
468                                 relative = true;
469                                 path = path.Substring (6);
470                             } else if (!path.StartsWith ("/")) {
471                                 relative = true;
472                             }
473
474                             return relative ? Path.Combine (home_dir, path) : path;
475                         }
476                                         }
477                                 }
478                         } catch (FileNotFoundException) {
479                         }
480
481                         return Path.Combine (home_dir, fallback);
482                 }
483
484
485                 // the security runtime (and maybe other parts of corlib) needs the
486                 // information to initialize themselves before permissions can be checked
487                 internal static string InternalGetFolderPath (SpecialFolder folder)
488                 {
489                         string home = internalGetHome ();
490
491                         // http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
492
493                         // note: skip security check for environment variables
494                         string data = internalGetEnvironmentVariable ("XDG_DATA_HOME");
495                         if ((data == null) || (data == String.Empty)) {
496                                 data = Path.Combine (home, ".local");
497                                 data = Path.Combine (data, "share");
498                         }
499
500                         // note: skip security check for environment variables
501                         string config = internalGetEnvironmentVariable ("XDG_CONFIG_HOME");
502                         if ((config == null) || (config == String.Empty)) {
503                                 config = Path.Combine (home, ".config");
504                         }
505
506                         switch (folder) {
507 #if NET_1_1
508                         // MyComputer is a virtual directory
509                         case SpecialFolder.MyComputer:
510                                 return String.Empty;
511 #endif
512                         // personal == ~
513                         case SpecialFolder.Personal:
514                                 return home;
515                         // use FDO's CONFIG_HOME. This data will be synced across a network like the windows counterpart.
516                         case SpecialFolder.ApplicationData:
517                                 return config;
518                         //use FDO's DATA_HOME. This is *NOT* synced
519                         case SpecialFolder.LocalApplicationData:
520                                 return data;
521 #if NET_1_1
522                         case SpecialFolder.Desktop:
523 #endif
524                         case SpecialFolder.DesktopDirectory:
525                                 return ReadXdgUserDir (config, home, "XDG_DESKTOP_DIR", "Desktop");
526
527                         case SpecialFolder.MyMusic:
528                                 return ReadXdgUserDir (config, home, "XDG_MUSIC_DIR", "Music");
529
530                         case SpecialFolder.MyPictures:
531                                 return ReadXdgUserDir (config, home, "XDG_PICTURES_DIR", "Pictures");
532                                 
533                         // these simply dont exist on Linux
534                         // The spec says if a folder doesnt exist, we
535                         // should return ""
536                         case SpecialFolder.Favorites:
537                         case SpecialFolder.Programs:
538                         case SpecialFolder.SendTo:
539                         case SpecialFolder.StartMenu:
540                         case SpecialFolder.Startup:
541                         case SpecialFolder.Templates:
542                         case SpecialFolder.Cookies:
543                         case SpecialFolder.History:
544                         case SpecialFolder.InternetCache:
545                         case SpecialFolder.Recent:
546                         case SpecialFolder.CommonProgramFiles:
547                         case SpecialFolder.ProgramFiles:
548                         case SpecialFolder.System:
549                                 return String.Empty;
550                         // This is where data common to all users goes
551                         case SpecialFolder.CommonApplicationData:
552                                 return "/usr/share";
553                         default:
554                                 throw new ArgumentException ("Invalid SpecialFolder");
555                         }
556                 }
557
558                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
559                 public static string[] GetLogicalDrives ()
560                 {
561                         return GetLogicalDrivesInternal ();
562                 }
563
564 #if NET_2_0
565                 public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
566                 {
567                         switch (target) {
568                         case EnvironmentVariableTarget.Process:
569                                 return GetEnvironmentVariable (variable);
570                         case EnvironmentVariableTarget.Machine:
571                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
572                                 if (!IsRunningOnWindows)
573                                         return null;
574                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
575                                         return env.GetValue (variable).ToString ();
576                                 }
577                         case EnvironmentVariableTarget.User:
578                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
579                                 if (!IsRunningOnWindows)
580                                         return null;
581                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", false)) {
582                                         return env.GetValue (variable).ToString ();
583                                 }
584                         default:
585                                 throw new ArgumentException ("target");
586                         }
587                 }
588
589                 public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
590                 {
591                         IDictionary variables = (IDictionary)new Hashtable ();
592                         switch (target) {
593                         case EnvironmentVariableTarget.Process:
594                                 variables = GetEnvironmentVariables ();
595                                 break;
596                         case EnvironmentVariableTarget.Machine:
597                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
598                                 if (IsRunningOnWindows) {
599                                         using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
600                                                 string[] value_names = env.GetValueNames ();
601                                                 foreach (string value_name in value_names)
602                                                         variables.Add (value_name, env.GetValue (value_name));
603                                         }
604                                 }
605                                 break;
606                         case EnvironmentVariableTarget.User:
607                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
608                                 if (IsRunningOnWindows) {
609                                         using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment")) {
610                                                 string[] value_names = env.GetValueNames ();
611                                                 foreach (string value_name in value_names)
612                                                         variables.Add (value_name, env.GetValue (value_name));
613                                         }
614                                 }
615                                 break;
616                         default:
617                                 throw new ArgumentException ("target");
618                         }
619                         return variables;
620                 }
621
622                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
623                 public static void SetEnvironmentVariable (string variable, string value)
624                 {
625                         SetEnvironmentVariable (variable, value, EnvironmentVariableTarget.Process);
626                 }
627
628                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted = true)]
629                 public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
630                 {
631                         if (variable == null)
632                                 throw new ArgumentNullException ("variable");
633                         if (variable == String.Empty)
634                                 throw new ArgumentException ("String cannot be of zero length.", "variable");
635                         if (variable.IndexOf ('=') != -1)
636                                 throw new ArgumentException ("Environment variable name cannot contain an equal character.", "variable");
637                         if (variable[0] == '\0')
638                                 throw new ArgumentException ("The first char in the string is the null character.", "variable");
639
640                         switch (target) {
641                         case EnvironmentVariableTarget.Process:
642                                 InternalSetEnvironmentVariable (variable, value);
643                                 break;
644                         case EnvironmentVariableTarget.Machine:
645                                 if (!IsRunningOnWindows)
646                                         return;
647                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) {
648                                         if (String.IsNullOrEmpty (value))
649                                                 env.DeleteValue (variable, false);
650                                         else
651                                                 env.SetValue (variable, value);
652                                 }
653                                 break;
654                         case EnvironmentVariableTarget.User:
655                                 if (!IsRunningOnWindows)
656                                         return;
657                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", true)) {
658                                         if (String.IsNullOrEmpty (value))
659                                                 env.DeleteValue (variable, false);
660                                         else
661                                                 env.SetValue (variable, value);
662                                 }
663                                 break;
664                         default:
665                                 throw new ArgumentException ("target");
666                         }
667                 }
668
669                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
670                 internal static extern void InternalSetEnvironmentVariable (string variable, string value);
671
672                 public static extern int ProcessorCount {
673                         [EnvironmentPermission (SecurityAction.Demand, Read="NUMBER_OF_PROCESSORS")]
674                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
675                         get;                    
676                 }
677
678                 [MonoTODO ("Not implemented")]
679                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
680                 public static void FailFast (string message)
681                 {
682                         throw new NotImplementedException ();
683                 }
684 #endif
685
686                 // private methods
687
688                 internal static bool IsRunningOnWindows {
689                         get { return ((int) Platform != 128); }
690                 }
691
692                 private static string GacPath {
693                         get {
694                                 if (Environment.IsRunningOnWindows) {
695                                         /* On windows, we don't know the path where mscorlib.dll will be installed */
696                                         string corlibDir = new DirectoryInfo (Path.GetDirectoryName (typeof (int).Assembly.Location)).Parent.Parent.FullName;
697                                         return Path.Combine (Path.Combine (corlibDir, "mono"), "gac");
698                                 }
699
700                                 return Path.Combine (Path.Combine (internalGetGacPath (), "mono"), "gac");
701                         }
702                 }
703
704                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
705                 private extern static string [] GetLogicalDrivesInternal ();
706
707                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
708                 private extern static string [] GetEnvironmentVariableNames ();
709
710                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
711                 internal extern static string GetMachineConfigPath ();
712
713                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
714                 internal extern static string internalGetGacPath ();
715
716                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
717                 internal extern static string internalGetHome ();
718         }
719 }
720