1 //------------------------------------------------------------------------------
3 // System.Environment.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created: Saturday, August 11, 2001
11 //------------------------------------------------------------------------------
13 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Security;
39 using System.Security.Permissions;
41 using System.Runtime.InteropServices;
42 using System.Threading;
43 using System.Diagnostics.Contracts;
48 public static partial class Environment {
51 * This is the version number of the corlib-runtime interface.
52 * It is defined in configure.ac.
54 #pragma warning disable 169
55 private const int mono_corlib_version = Consts.MonoCorlibVersion;
56 #pragma warning restore 169
59 public enum SpecialFolder
72 DesktopDirectory = 0x10,
74 ApplicationData = 0x1a,
75 LocalApplicationData = 0x1c,
79 CommonApplicationData = 0x23,
83 CommonProgramFiles = 0x2b,
85 NetworkShortcuts = 0x13,
87 CommonStartMenu = 0x16,
88 CommonPrograms = 0x17,
90 CommonDesktopDirectory = 0x19,
91 PrinterShortcuts = 0x1b,
95 ProgramFilesX86 = 0x2a,
96 CommonProgramFilesX86 = 0x2c,
97 CommonTemplates = 0x2d,
98 CommonDocuments = 0x2e,
99 CommonAdminTools = 0x2f,
102 CommonPictures = 0x36,
105 LocalizedResources = 0x39,
106 CommonOemLinks = 0x3a,
111 enum SpecialFolderOption {
113 DoNotVerify = 0x4000,
118 /// Gets the command line for this process
120 public static string CommandLine {
121 // note: security demand inherited from calling GetCommandLineArgs
123 StringBuilder sb = new StringBuilder ();
124 foreach (string str in GetCommandLineArgs ()) {
128 for (int i = 0; i < s.Length; i++) {
129 if (quote.Length == 0 && Char.IsWhiteSpace (s [i])) {
131 } else if (s [i] == '"') {
135 if (escape && quote.Length != 0) {
136 s = s.Replace ("\"", "\\\"");
138 sb.AppendFormat ("{0}{1}{0} ", quote, s);
142 return sb.ToString ();
147 /// Gets or sets the current directory. Actually this is supposed to get
148 /// and/or set the process start directory acording to the documentation
149 /// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
151 public static string CurrentDirectory
154 return Directory.GetCurrentDirectory ();
157 Directory.SetCurrentDirectory (value);
161 public static int CurrentManagedThreadId {
163 return Thread.CurrentThread.ManagedThreadId;
168 /// Gets or sets the exit code of this process
170 public extern static int ExitCode
172 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174 [MethodImplAttribute (MethodImplOptions.InternalCall)]
178 static public extern bool HasShutdownStarted
180 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186 /// Gets the name of the local computer
188 public extern static string MachineName {
189 [MethodImplAttribute (MethodImplOptions.InternalCall)]
190 [EnvironmentPermission (SecurityAction.Demand, Read="COMPUTERNAME")]
191 [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
195 [MethodImplAttribute (MethodImplOptions.InternalCall)]
196 extern static string GetNewLine ();
200 /// Gets the standard new line value
202 public static string NewLine {
213 // Support methods and fields for OSVersion property
215 static OperatingSystem os;
217 static extern PlatformID Platform {
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
222 [MethodImplAttribute (MethodImplOptions.InternalCall)]
223 internal static extern string GetOSVersionString ();
226 /// Gets the current OS version information
228 public static OperatingSystem OSVersion {
231 Version v = CreateVersionFromString (GetOSVersionString ());
232 PlatformID p = Platform;
233 if (p == PlatformID.MacOSX)
235 os = new OperatingSystem (p, v);
242 // a very gentle way to construct a Version object which takes
243 // the first four numbers in a string as the version
244 internal static Version CreateVersionFromString (string info)
251 int number = -1; // string may not begin with a digit
254 return new Version (0, 0, 0, 0);
256 for (int i=0; i < info.Length; i++) {
258 if (Char.IsDigit (c)) {
263 number = (number * 10) + (c - '0');
266 else if (number >= 0) {
285 // ignore end of string
307 return new Version (major, minor, build, revision);
313 public static string StackTrace {
314 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
316 System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (0, true);
317 return trace.ToString ();
322 /// Get a fully qualified path to the system directory
324 public static string SystemDirectory {
326 return GetFolderPath (SpecialFolder.System);
331 /// Get the number of milliseconds that have elapsed since the system was booted
333 public extern static int TickCount {
334 [MethodImplAttribute (MethodImplOptions.InternalCall)]
339 /// Get UserDomainName
341 public static string UserDomainName {
342 // FIXME: this variable doesn't exist (at least not on WinXP) - reported to MS as FDBK20562
343 [EnvironmentPermission (SecurityAction.Demand, Read="USERDOMAINNAME")]
350 /// Gets a flag indicating whether the process is in interactive mode
352 [MonoTODO ("Currently always returns false, regardless of interactive state")]
353 public static bool UserInteractive {
360 /// Get the user name of current process is running under
362 public extern static string UserName {
363 [MethodImplAttribute (MethodImplOptions.InternalCall)]
364 [EnvironmentPermission (SecurityAction.Demand, Read="USERNAME;USER")]
369 /// Get the version of the common language runtime
371 public static Version Version {
373 return new Version (Consts.EnvironmentVersion);
378 /// Get the amount of physical memory mapped to process
380 [MonoTODO ("Currently always returns zero")]
381 public static long WorkingSet {
382 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
386 [MethodImplAttribute (MethodImplOptions.InternalCall)]
387 [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
388 public extern static void Exit (int exitCode);
390 internal static void _Exit (int exitCode)
396 /// Substitute environment variables in the argument "name"
398 public static string ExpandEnvironmentVariables (string name)
401 throw new ArgumentNullException ("name");
403 int off1 = name.IndexOf ('%');
407 int len = name.Length;
409 if (off1 == len - 1 || (off2 = name.IndexOf ('%', off1 + 1)) == -1)
412 StringBuilder result = new StringBuilder ();
413 result.Append (name, 0, off1);
414 Hashtable tbl = null;
416 string var = name.Substring (off1 + 1, off2 - off1 - 1);
417 string value = GetEnvironmentVariable (var);
418 if (value == null && Environment.IsRunningOnWindows) {
419 // On windows, env. vars. are case insensitive
421 tbl = GetEnvironmentVariablesNoCase ();
423 value = tbl [var] as string;
426 // If value not found, add %FOO to stream,
427 // and use the closing % for the next iteration.
428 // If value found, expand it in place of %FOO%
429 int realOldOff2 = off2;
435 result.Append (value);
438 off1 = name.IndexOf ('%', off2 + 1);
439 // If no % found for off1, don't look for one for off2
440 off2 = (off1 == -1 || off2 > len-1)? -1 :name.IndexOf ('%', off1 + 1);
441 // textLen is the length of text between the closing % of current iteration
442 // and the starting % of the next iteration if any. This text is added to output
444 // If no new % found, use all the remaining text
445 if (off1 == -1 || off2 == -1)
446 textLen = len - oldOff2 - 1;
447 // If value found in current iteration, use text after current closing % and next %
448 else if(value != null)
449 textLen = off1 - oldOff2 - 1;
450 // If value not found in current iteration, but a % was found for next iteration,
451 // use text from current closing % to the next %.
453 textLen = off1 - realOldOff2;
454 if(off1 >= oldOff2 || off1 == -1)
455 result.Append (name, oldOff2+1, textLen);
456 } while (off2 > -1 && off2 < len);
458 return result.ToString ();
463 /// Return an array of the command line arguments of the current process
465 [MethodImplAttribute (MethodImplOptions.InternalCall)]
466 [EnvironmentPermissionAttribute (SecurityAction.Demand, Read = "PATH")]
467 public extern static string[] GetCommandLineArgs ();
469 [MethodImplAttribute (MethodImplOptions.InternalCall)]
470 internal extern static string internalGetEnvironmentVariable_native (IntPtr variable);
472 internal static string internalGetEnvironmentVariable (string variable) {
473 if (variable == null)
475 using (var h = Mono.RuntimeMarshal.MarshalString (variable)) {
476 return internalGetEnvironmentVariable_native (h.Value);
481 /// Return a string containing the value of the environment
482 /// variable identifed by parameter "variable"
484 public static string GetEnvironmentVariable (string variable)
487 if (SecurityManager.SecurityEnabled) {
488 new EnvironmentPermission (EnvironmentPermissionAccess.Read, variable).Demand ();
491 return internalGetEnvironmentVariable (variable);
494 static Hashtable GetEnvironmentVariablesNoCase ()
496 Hashtable vars = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
497 CaseInsensitiveComparer.Default);
499 foreach (string name in GetEnvironmentVariableNames ()) {
500 vars [name] = internalGetEnvironmentVariable (name);
507 /// Return a set of all environment variables and their values
510 public static IDictionary GetEnvironmentVariables ()
512 StringBuilder sb = null;
513 if (SecurityManager.SecurityEnabled) {
514 // we must have access to each variable to get the lot
515 sb = new StringBuilder ();
516 // but (performance-wise) we do not want a stack-walk
517 // for each of them so we concatenate them
520 Hashtable vars = new Hashtable ();
521 foreach (string name in GetEnvironmentVariableNames ()) {
522 vars [name] = internalGetEnvironmentVariable (name);
530 new EnvironmentPermission (EnvironmentPermissionAccess.Read, sb.ToString ()).Demand ();
535 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
536 public static IDictionary GetEnvironmentVariables ()
538 Hashtable vars = new Hashtable ();
539 foreach (string name in GetEnvironmentVariableNames ()) {
540 vars [name] = internalGetEnvironmentVariable (name);
547 /// Returns the fully qualified path of the
548 /// folder specified by the "folder" parameter
550 public static string GetFolderPath (SpecialFolder folder)
552 return GetFolderPath (folder, SpecialFolderOption.None);
556 [MethodImplAttribute (MethodImplOptions.InternalCall)]
557 private extern static string GetWindowsFolderPath (int folder);
560 static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
562 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
566 if (Environment.IsRunningOnWindows)
567 dir = GetWindowsFolderPath ((int) folder);
569 dir = UnixGetFolderPath (folder, option);
572 if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
573 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
579 private static string ReadXdgUserDir (string config_dir, string home_dir, string key, string fallback)
581 string env_path = internalGetEnvironmentVariable (key);
582 if (env_path != null && env_path != String.Empty) {
586 string user_dirs_path = Path.Combine (config_dir, "user-dirs.dirs");
588 if (!File.Exists (user_dirs_path)) {
589 return Path.Combine (home_dir, fallback);
593 using(StreamReader reader = new StreamReader (user_dirs_path)) {
595 while ((line = reader.ReadLine ()) != null) {
597 int delim_index = line.IndexOf ('=');
598 if(delim_index > 8 && line.Substring (0, delim_index) == key) {
599 string path = line.Substring (delim_index + 1).Trim ('"');
600 bool relative = false;
602 if (path.StartsWithOrdinalUnchecked ("$HOME/")) {
604 path = path.Substring (6);
605 } else if (!path.StartsWithOrdinalUnchecked ("/")) {
609 return relative ? Path.Combine (home_dir, path) : path;
613 } catch (FileNotFoundException) {
616 return Path.Combine (home_dir, fallback);
620 // the security runtime (and maybe other parts of corlib) needs the
621 // information to initialize themselves before permissions can be checked
622 internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
624 string home = internalGetHome ();
626 // http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
628 // note: skip security check for environment variables
629 string data = internalGetEnvironmentVariable ("XDG_DATA_HOME");
630 if ((data == null) || (data == String.Empty)) {
631 data = Path.Combine (home, ".local");
632 data = Path.Combine (data, "share");
635 // note: skip security check for environment variables
636 string config = internalGetEnvironmentVariable ("XDG_CONFIG_HOME");
637 if ((config == null) || (config == String.Empty)) {
638 config = Path.Combine (home, ".config");
642 // MyComputer is a virtual directory
643 case SpecialFolder.MyComputer:
647 case SpecialFolder.Personal:
650 // use FDO's CONFIG_HOME. This data will be synced across a network like the windows counterpart.
651 case SpecialFolder.ApplicationData:
654 //use FDO's DATA_HOME. This is *NOT* synced
655 case SpecialFolder.LocalApplicationData:
658 case SpecialFolder.Desktop:
659 case SpecialFolder.DesktopDirectory:
660 return ReadXdgUserDir (config, home, "XDG_DESKTOP_DIR", "Desktop");
662 case SpecialFolder.MyMusic:
663 if (Platform == PlatformID.MacOSX)
664 return Path.Combine (home, "Music");
666 return ReadXdgUserDir (config, home, "XDG_MUSIC_DIR", "Music");
668 case SpecialFolder.MyPictures:
669 if (Platform == PlatformID.MacOSX)
670 return Path.Combine (home, "Pictures");
672 return ReadXdgUserDir (config, home, "XDG_PICTURES_DIR", "Pictures");
674 case SpecialFolder.Templates:
675 return ReadXdgUserDir (config, home, "XDG_TEMPLATES_DIR", "Templates");
676 case SpecialFolder.MyVideos:
677 return ReadXdgUserDir (config, home, "XDG_VIDEOS_DIR", "Videos");
678 case SpecialFolder.CommonTemplates:
679 return "/usr/share/templates";
680 case SpecialFolder.Fonts:
681 if (Platform == PlatformID.MacOSX)
682 return Path.Combine (home, "Library", "Fonts");
684 return Path.Combine (home, ".fonts");
685 // these simply dont exist on Linux
686 // The spec says if a folder doesnt exist, we
688 case SpecialFolder.Favorites:
689 if (Platform == PlatformID.MacOSX)
690 return Path.Combine (home, "Library", "Favorites");
694 case SpecialFolder.ProgramFiles:
695 if (Platform == PlatformID.MacOSX)
696 return "/Applications";
700 case SpecialFolder.InternetCache:
701 if (Platform == PlatformID.MacOSX)
702 return Path.Combine (home, "Library", "Caches");
707 case SpecialFolder.UserProfile:
710 case SpecialFolder.Programs:
711 case SpecialFolder.SendTo:
712 case SpecialFolder.StartMenu:
713 case SpecialFolder.Startup:
714 case SpecialFolder.Cookies:
715 case SpecialFolder.History:
716 case SpecialFolder.Recent:
717 case SpecialFolder.CommonProgramFiles:
718 case SpecialFolder.System:
719 case SpecialFolder.NetworkShortcuts:
720 case SpecialFolder.CommonStartMenu:
721 case SpecialFolder.CommonPrograms:
722 case SpecialFolder.CommonStartup:
723 case SpecialFolder.CommonDesktopDirectory:
724 case SpecialFolder.PrinterShortcuts:
725 case SpecialFolder.Windows:
726 case SpecialFolder.SystemX86:
727 case SpecialFolder.ProgramFilesX86:
728 case SpecialFolder.CommonProgramFilesX86:
729 case SpecialFolder.CommonDocuments:
730 case SpecialFolder.CommonAdminTools:
731 case SpecialFolder.AdminTools:
732 case SpecialFolder.CommonMusic:
733 case SpecialFolder.CommonPictures:
734 case SpecialFolder.CommonVideos:
735 case SpecialFolder.Resources:
736 case SpecialFolder.LocalizedResources:
737 case SpecialFolder.CommonOemLinks:
738 case SpecialFolder.CDBurning:
740 // This is where data common to all users goes
741 case SpecialFolder.CommonApplicationData:
744 throw new ArgumentException ("Invalid SpecialFolder");
750 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
751 public static string[] GetLogicalDrives ()
753 return GetLogicalDrivesInternal ();
757 [MethodImplAttribute (MethodImplOptions.InternalCall)]
758 private static extern void internalBroadcastSettingChange ();
760 public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
763 case EnvironmentVariableTarget.Process:
764 return GetEnvironmentVariable (variable);
765 case EnvironmentVariableTarget.Machine:
766 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
767 if (!IsRunningOnWindows)
769 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
770 object regvalue = env.GetValue (variable);
771 return (regvalue == null) ? null : regvalue.ToString ();
773 case EnvironmentVariableTarget.User:
774 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
775 if (!IsRunningOnWindows)
777 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", false)) {
778 object regvalue = env.GetValue (variable);
779 return (regvalue == null) ? null : regvalue.ToString ();
782 throw new ArgumentException ("target");
786 public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
788 IDictionary variables = (IDictionary)new Hashtable ();
790 case EnvironmentVariableTarget.Process:
791 variables = GetEnvironmentVariables ();
793 case EnvironmentVariableTarget.Machine:
794 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
795 if (IsRunningOnWindows) {
796 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
797 string[] value_names = env.GetValueNames ();
798 foreach (string value_name in value_names)
799 variables.Add (value_name, env.GetValue (value_name));
803 case EnvironmentVariableTarget.User:
804 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
805 if (IsRunningOnWindows) {
806 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment")) {
807 string[] value_names = env.GetValueNames ();
808 foreach (string value_name in value_names)
809 variables.Add (value_name, env.GetValue (value_name));
814 throw new ArgumentException ("target");
819 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
820 public static void SetEnvironmentVariable (string variable, string value)
822 SetEnvironmentVariable (variable, value, EnvironmentVariableTarget.Process);
825 [EnvironmentPermission (SecurityAction.Demand, Unrestricted = true)]
826 public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
828 if (variable == null)
829 throw new ArgumentNullException ("variable");
830 if (variable == String.Empty)
831 throw new ArgumentException ("String cannot be of zero length.", "variable");
832 if (variable.IndexOf ('=') != -1)
833 throw new ArgumentException ("Environment variable name cannot contain an equal character.", "variable");
834 if (variable[0] == '\0')
835 throw new ArgumentException ("The first char in the string is the null character.", "variable");
838 case EnvironmentVariableTarget.Process:
839 InternalSetEnvironmentVariable (variable, value);
841 case EnvironmentVariableTarget.Machine:
842 if (!IsRunningOnWindows)
844 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) {
845 if (String.IsNullOrEmpty (value))
846 env.DeleteValue (variable, false);
848 env.SetValue (variable, value);
849 internalBroadcastSettingChange ();
852 case EnvironmentVariableTarget.User:
853 if (!IsRunningOnWindows)
855 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", true)) {
856 if (String.IsNullOrEmpty (value))
857 env.DeleteValue (variable, false);
859 env.SetValue (variable, value);
860 internalBroadcastSettingChange ();
864 throw new ArgumentException ("target");
868 public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
870 if (target == EnvironmentVariableTarget.Process)
871 return GetEnvironmentVariable (variable);
876 public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
878 if (target == EnvironmentVariableTarget.Process)
879 return GetEnvironmentVariables ();
881 return (IDictionary)new Hashtable ();
884 public static void SetEnvironmentVariable (string variable, string value)
886 if (variable == null)
887 throw new ArgumentNullException ("variable");
888 if (variable == String.Empty)
889 throw new ArgumentException ("String cannot be of zero length.", "variable");
890 if (variable.IndexOf ('=') != -1)
891 throw new ArgumentException ("Environment variable name cannot contain an equal character.", "variable");
892 if (variable[0] == '\0')
893 throw new ArgumentException ("The first char in the string is the null character.", "variable");
895 InternalSetEnvironmentVariable (variable, value);
898 public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
900 if (target == EnvironmentVariableTarget.Process)
901 SetEnvironmentVariable (variable, value);
903 // other targets ignored
906 [MethodImplAttribute (MethodImplOptions.InternalCall)]
907 internal static extern void InternalSetEnvironmentVariable (string variable, string value);
909 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
910 public static void FailFast (string message)
912 throw new NotImplementedException ();
915 internal static void FailFast (String message, uint exitCode)
917 throw new NotImplementedException ();
921 public static void FailFast (string message, Exception exception)
923 #pragma warning disable 618
924 throw new ExecutionEngineException (message, exception);
925 #pragma warning restore
928 [MethodImplAttribute (MethodImplOptions.InternalCall)]
929 extern static bool GetIs64BitOperatingSystem ();
931 public static bool Is64BitOperatingSystem {
932 get { return GetIs64BitOperatingSystem (); }
935 public static int SystemPageSize {
936 get { return GetPageSize (); }
940 static bool Is64BitProcess {
941 get { return IntPtr.Size == 8; }
944 public static extern int ProcessorCount {
945 [EnvironmentPermission (SecurityAction.Demand, Read="NUMBER_OF_PROCESSORS")]
946 [MethodImplAttribute (MethodImplOptions.InternalCall)]
951 #if (MONOTOUCH || MONODROID || XAMMAC)
952 internal const bool IsRunningOnWindows = false;
954 internal static bool IsRunningOnWindows {
955 get { return ((int) Platform < 4); }
961 // Used by gacutil.exe
963 #pragma warning disable 169
964 private static string GacPath {
966 if (Environment.IsRunningOnWindows) {
967 /* On windows, we don't know the path where mscorlib.dll will be installed */
968 string corlibDir = new DirectoryInfo (Path.GetDirectoryName (typeof (int).Assembly.Location)).Parent.Parent.FullName;
969 return Path.Combine (Path.Combine (corlibDir, "mono"), "gac");
972 return Path.Combine (Path.Combine (internalGetGacPath (), "mono"), "gac");
975 #pragma warning restore 169
976 [MethodImplAttribute (MethodImplOptions.InternalCall)]
977 internal extern static string internalGetGacPath ();
979 [MethodImplAttribute (MethodImplOptions.InternalCall)]
980 private extern static string [] GetLogicalDrivesInternal ();
982 [MethodImplAttribute (MethodImplOptions.InternalCall)]
983 private extern static string [] GetEnvironmentVariableNames ();
985 [MethodImplAttribute (MethodImplOptions.InternalCall)]
986 internal extern static string GetMachineConfigPath ();
988 [MethodImplAttribute (MethodImplOptions.InternalCall)]
989 internal extern static string internalGetHome ();
991 [MethodImplAttribute (MethodImplOptions.InternalCall)]
992 internal extern static int GetPageSize ();
994 [MethodImplAttribute(MethodImplOptions.InternalCall)]
995 extern private static string get_bundled_machine_config ();
997 internal static string GetBundledMachineConfig ()
999 return get_bundled_machine_config ();
1002 static internal bool IsUnix {
1004 int platform = (int) Environment.Platform;
1006 return (platform == 4 || platform == 128 || platform == 6);
1009 static internal bool IsMacOS {
1011 return Environment.Platform == PlatformID.MacOSX;
1015 internal static bool IsCLRHosted {
1021 internal static void TriggerCodeContractFailure(ContractFailureKind failureKind, String message, String condition, String exceptionAsString)
1026 // Copied from referencesource Environment
1027 internal static String GetStackTrace(Exception e, bool needFileInfo)
1029 System.Diagnostics.StackTrace st;
1031 st = new System.Diagnostics.StackTrace(needFileInfo);
1033 st = new System.Diagnostics.StackTrace(e, needFileInfo);
1035 // Do not include a trailing newline for backwards compatibility
1036 return st.ToString( System.Diagnostics.StackTrace.TraceFormat.Normal );
1039 // Copied from referencesource Environment
1040 internal static bool IsWinRTSupported