Environment.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 int ProcessorCount { get; } = GetProcessorCount();
  13. /// <summary>
  14. /// Gets whether the current machine has only a single processor.
  15. /// </summary>
  16. internal static bool IsSingleProcessor => ProcessorCount == 1;
  17. // Unconditionally return false since .NET Core does not support object finalization during shutdown.
  18. public static bool HasShutdownStarted => false;
  19. public static string? GetEnvironmentVariable(string variable)
  20. {
  21. if (variable == null)
  22. throw new ArgumentNullException(nameof(variable));
  23. return GetEnvironmentVariableCore(variable);
  24. }
  25. public static string? GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
  26. {
  27. if (target == EnvironmentVariableTarget.Process)
  28. return GetEnvironmentVariable(variable);
  29. if (variable == null)
  30. throw new ArgumentNullException(nameof(variable));
  31. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  32. return GetEnvironmentVariableFromRegistry(variable, fromMachine);
  33. }
  34. public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
  35. {
  36. if (target == EnvironmentVariableTarget.Process)
  37. return GetEnvironmentVariables();
  38. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  39. return GetEnvironmentVariablesFromRegistry(fromMachine);
  40. }
  41. public static void SetEnvironmentVariable(string variable, string? value)
  42. {
  43. ValidateVariableAndValue(variable, ref value);
  44. SetEnvironmentVariableCore(variable, value);
  45. }
  46. public static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
  47. {
  48. if (target == EnvironmentVariableTarget.Process)
  49. {
  50. SetEnvironmentVariable(variable, value);
  51. return;
  52. }
  53. ValidateVariableAndValue(variable, ref value);
  54. bool fromMachine = ValidateAndConvertRegistryTarget(target);
  55. SetEnvironmentVariableFromRegistry(variable, value, fromMachine: fromMachine);
  56. }
  57. public static string CommandLine => PasteArguments.Paste(GetCommandLineArgs(), pasteFirstArgumentUsingArgV0Rules: true);
  58. public static string CurrentDirectory
  59. {
  60. get => CurrentDirectoryCore;
  61. set
  62. {
  63. if (value == null)
  64. throw new ArgumentNullException(nameof(value));
  65. if (value.Length == 0)
  66. throw new ArgumentException(SR.Argument_PathEmpty, nameof(value));
  67. CurrentDirectoryCore = value;
  68. }
  69. }
  70. public static string ExpandEnvironmentVariables(string name)
  71. {
  72. if (name == null)
  73. throw new ArgumentNullException(nameof(name));
  74. if (name.Length == 0)
  75. return name;
  76. return ExpandEnvironmentVariablesCore(name);
  77. }
  78. private static string[]? s_commandLineArgs;
  79. internal static void SetCommandLineArgs(string[] cmdLineArgs) // invoked from VM
  80. {
  81. s_commandLineArgs = cmdLineArgs;
  82. }
  83. public static string GetFolderPath(SpecialFolder folder) => GetFolderPath(folder, SpecialFolderOption.None);
  84. public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
  85. {
  86. if (!Enum.IsDefined(typeof(SpecialFolder), folder))
  87. throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
  88. if (option != SpecialFolderOption.None && !Enum.IsDefined(typeof(SpecialFolderOption), option))
  89. throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
  90. return GetFolderPathCore(folder, option);
  91. }
  92. public static bool Is64BitProcess => IntPtr.Size == 8;
  93. public static bool Is64BitOperatingSystem => Is64BitProcess || Is64BitOperatingSystemWhen32BitProcess;
  94. public static string NewLine => NewLineConst;
  95. private static OperatingSystem? s_osVersion;
  96. public static OperatingSystem OSVersion
  97. {
  98. get
  99. {
  100. if (s_osVersion == null)
  101. {
  102. Interlocked.CompareExchange(ref s_osVersion, GetOSVersion(), null);
  103. }
  104. return s_osVersion;
  105. }
  106. }
  107. public static bool UserInteractive => true;
  108. public static Version Version
  109. {
  110. get
  111. {
  112. // FX_PRODUCT_VERSION is expected to be set by the host
  113. // Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host
  114. string? versionString = (string?)AppContext.GetData("FX_PRODUCT_VERSION") ??
  115. typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
  116. ReadOnlySpan<char> versionSpan = versionString.AsSpan();
  117. // Strip optional suffixes
  118. int separatorIndex = versionSpan.IndexOfAny("-+ ");
  119. if (separatorIndex != -1)
  120. versionSpan = versionSpan.Slice(0, separatorIndex);
  121. // Return zeros rather then failing if the version string fails to parse
  122. return Version.TryParse(versionSpan, out Version? version) ? version : new Version();
  123. }
  124. }
  125. private static bool ValidateAndConvertRegistryTarget(EnvironmentVariableTarget target)
  126. {
  127. Debug.Assert(target != EnvironmentVariableTarget.Process);
  128. if (target == EnvironmentVariableTarget.Machine)
  129. return true;
  130. if (target == EnvironmentVariableTarget.User)
  131. return false;
  132. throw new ArgumentOutOfRangeException(nameof(target), target, SR.Format(SR.Arg_EnumIllegalVal, target));
  133. }
  134. private static void ValidateVariableAndValue(string variable, ref string? value)
  135. {
  136. if (variable == null)
  137. throw new ArgumentNullException(nameof(variable));
  138. if (variable.Length == 0)
  139. throw new ArgumentException(SR.Argument_StringZeroLength, nameof(variable));
  140. if (variable[0] == '\0')
  141. throw new ArgumentException(SR.Argument_StringFirstCharIsZero, nameof(variable));
  142. if (variable.Contains('='))
  143. throw new ArgumentException(SR.Argument_IllegalEnvVarName, nameof(variable));
  144. if (string.IsNullOrEmpty(value) || value[0] == '\0')
  145. {
  146. // Explicitly null out value if it's empty
  147. value = null;
  148. }
  149. }
  150. }
  151. }