Environment.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using System.Threading;
  8. namespace System
  9. {
  10. public static partial class Environment
  11. {
  12. public static string? GetEnvironmentVariable(string variable)
  13. {
  14. if (variable == null)
  15. throw new ArgumentNullException(nameof(variable));
  16. return GetEnvironmentVariableCore(variable);
  17. }
  18. public static string? GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
  19. {
  20. if (target == EnvironmentVariableTarget.Process)
  21. return GetEnvironmentVariable(variable);
  22. if (variable == null)
  23. throw new ArgumentNullException(nameof(variable));
  24. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  25. return GetEnvironmentVariableFromRegistry(variable, fromMachine);
  26. }
  27. public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
  28. {
  29. if (target == EnvironmentVariableTarget.Process)
  30. return GetEnvironmentVariables();
  31. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  32. return GetEnvironmentVariablesFromRegistry(fromMachine);
  33. }
  34. public static void SetEnvironmentVariable(string variable, string? value)
  35. {
  36. ValidateVariableAndValue(variable, ref value);
  37. SetEnvironmentVariableCore(variable, value);
  38. }
  39. public static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
  40. {
  41. if (target == EnvironmentVariableTarget.Process)
  42. {
  43. SetEnvironmentVariable(variable, value);
  44. return;
  45. }
  46. ValidateVariableAndValue(variable, ref value);
  47. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  48. SetEnvironmentVariableFromRegistry(variable, value, fromMachine: fromMachine);
  49. }
  50. public static string CommandLine => PasteArguments.Paste(GetCommandLineArgs(), pasteFirstArgumentUsingArgV0Rules: true);
  51. public static string CurrentDirectory
  52. {
  53. get => CurrentDirectoryCore;
  54. set
  55. {
  56. if (value == null)
  57. throw new ArgumentNullException(nameof(value));
  58. if (value.Length == 0)
  59. throw new ArgumentException(SR.Argument_PathEmpty, nameof(value));
  60. CurrentDirectoryCore = value;
  61. }
  62. }
  63. public static string ExpandEnvironmentVariables(string name)
  64. {
  65. if (name == null)
  66. throw new ArgumentNullException(nameof(name));
  67. if (name.Length == 0)
  68. return name;
  69. return ExpandEnvironmentVariablesCore(name);
  70. }
  71. private static string[]? s_commandLineArgs;
  72. internal static void SetCommandLineArgs(string[] cmdLineArgs) // invoked from VM
  73. {
  74. s_commandLineArgs = cmdLineArgs;
  75. }
  76. public static string GetFolderPath(SpecialFolder folder) => GetFolderPath(folder, SpecialFolderOption.None);
  77. public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
  78. {
  79. if (!Enum.IsDefined(typeof(SpecialFolder), folder))
  80. throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
  81. if (option != SpecialFolderOption.None && !Enum.IsDefined(typeof(SpecialFolderOption), option))
  82. throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
  83. return GetFolderPathCore(folder, option);
  84. }
  85. public static bool Is64BitProcess => IntPtr.Size == 8;
  86. public static bool Is64BitOperatingSystem => Is64BitProcess || Is64BitOperatingSystemWhen32BitProcess;
  87. private static OperatingSystem? s_osVersion;
  88. public static OperatingSystem OSVersion
  89. {
  90. get
  91. {
  92. if (s_osVersion == null)
  93. {
  94. Interlocked.CompareExchange(ref s_osVersion, GetOSVersion(), null);
  95. }
  96. return s_osVersion;
  97. }
  98. }
  99. public static bool UserInteractive => true;
  100. public static Version Version
  101. {
  102. get
  103. {
  104. // FX_PRODUCT_VERSION is expected to be set by the host
  105. string? versionString = (string?)AppContext.GetData("FX_PRODUCT_VERSION");
  106. if (versionString == null)
  107. {
  108. // Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host
  109. versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
  110. }
  111. ReadOnlySpan<char> versionSpan = versionString.AsSpan();
  112. // Strip optional suffixes
  113. int separatorIndex = versionSpan.IndexOfAny("-+ ");
  114. if (separatorIndex != -1)
  115. versionSpan = versionSpan.Slice(0, separatorIndex);
  116. // Return zeros rather then failing if the version string fails to parse
  117. return Version.TryParse(versionSpan, out Version? version) ? version : new Version();
  118. }
  119. }
  120. public static long WorkingSet
  121. {
  122. get
  123. {
  124. // Use reflection to access the implementation in System.Diagnostics.Process.dll. While far from ideal,
  125. // we do this to avoid duplicating the Windows, Linux, macOS, and potentially other platform-specific implementations
  126. // present in Process. If it proves important, we could look at separating that functionality out of Process into
  127. // Common files which could also be included here.
  128. Type? processType = Type.GetType("System.Diagnostics.Process, System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false);
  129. IDisposable? currentProcess = processType?.GetMethod("GetCurrentProcess")?.Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null) as IDisposable;
  130. if (currentProcess != null)
  131. {
  132. using (currentProcess)
  133. {
  134. object? result = processType!.GetMethod("get_WorkingSet64")?.Invoke(currentProcess, BindingFlags.DoNotWrapExceptions, null, null, null);
  135. if (result is long) return (long)result;
  136. }
  137. }
  138. // Could not get the current working set.
  139. return 0;
  140. }
  141. }
  142. private static bool ValidateAndConvertRegistryTarget(EnvironmentVariableTarget target)
  143. {
  144. Debug.Assert(target != EnvironmentVariableTarget.Process);
  145. if (target == EnvironmentVariableTarget.Machine)
  146. return true;
  147. if (target == EnvironmentVariableTarget.User)
  148. return false;
  149. throw new ArgumentOutOfRangeException(nameof(target), target, SR.Format(SR.Arg_EnumIllegalVal, target));
  150. }
  151. private static void ValidateVariableAndValue(string variable, ref string? value)
  152. {
  153. if (variable == null)
  154. throw new ArgumentNullException(nameof(variable));
  155. if (variable.Length == 0)
  156. throw new ArgumentException(SR.Argument_StringZeroLength, nameof(variable));
  157. if (variable[0] == '\0')
  158. throw new ArgumentException(SR.Argument_StringFirstCharIsZero, nameof(variable));
  159. if (variable.Contains('='))
  160. throw new ArgumentException(SR.Argument_IllegalEnvVarName, nameof(variable));
  161. if (string.IsNullOrEmpty(value) || value[0] == '\0')
  162. {
  163. // Explicitly null out value if it's empty
  164. value = null;
  165. }
  166. }
  167. }
  168. }