Environment.Windows.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using Internal.Runtime.Augments;
  8. namespace System
  9. {
  10. public static partial class Environment
  11. {
  12. private static string CurrentDirectoryCore
  13. {
  14. get
  15. {
  16. Span<char> initialBuffer = stackalloc char[Interop.Kernel32.MAX_PATH];
  17. var builder = new ValueStringBuilder(initialBuffer);
  18. uint length;
  19. while ((length = Interop.Kernel32.GetCurrentDirectory((uint)builder.Capacity, ref builder.GetPinnableReference())) > builder.Capacity)
  20. {
  21. builder.EnsureCapacity((int)length);
  22. }
  23. if (length == 0)
  24. throw Win32Marshal.GetExceptionForLastWin32Error();
  25. builder.Length = (int)length;
  26. // If we have a tilde in the path, make an attempt to expand 8.3 filenames
  27. return builder.AsSpan().Contains('~')
  28. ? PathHelper.TryExpandShortFileName(ref builder, null)
  29. : builder.ToString();
  30. }
  31. set
  32. {
  33. if (!Interop.Kernel32.SetCurrentDirectory(value))
  34. {
  35. int errorCode = Marshal.GetLastWin32Error();
  36. throw Win32Marshal.GetExceptionForWin32Error(
  37. errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND ? Interop.Errors.ERROR_PATH_NOT_FOUND : errorCode,
  38. value);
  39. }
  40. }
  41. }
  42. public static string[] GetLogicalDrives() => DriveInfoInternal.GetLogicalDrives();
  43. public static string NewLine => "\r\n";
  44. public static int SystemPageSize
  45. {
  46. get
  47. {
  48. Interop.Kernel32.GetSystemInfo(out Interop.Kernel32.SYSTEM_INFO info);
  49. return info.dwPageSize;
  50. }
  51. }
  52. public static int ExitCode { get { return EnvironmentAugments.ExitCode; } set { EnvironmentAugments.ExitCode = value; } }
  53. private static string ExpandEnvironmentVariablesCore(string name)
  54. {
  55. Span<char> initialBuffer = stackalloc char[128];
  56. var builder = new ValueStringBuilder(initialBuffer);
  57. uint length;
  58. while ((length = Interop.Kernel32.ExpandEnvironmentStringsW(name, ref builder.GetPinnableReference(), (uint)builder.Capacity)) > builder.Capacity)
  59. {
  60. builder.EnsureCapacity((int)length);
  61. }
  62. if (length == 0)
  63. Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
  64. // length includes the null terminator
  65. builder.Length = (int)length - 1;
  66. return builder.ToString();
  67. }
  68. private static bool Is64BitOperatingSystemWhen32BitProcess
  69. => Interop.Kernel32.IsWow64Process(Interop.Kernel32.GetCurrentProcess(), out bool isWow64) && isWow64;
  70. public static string MachineName
  71. {
  72. get
  73. {
  74. string name = Interop.Kernel32.GetComputerName();
  75. if (name == null)
  76. {
  77. throw new InvalidOperationException(SR.InvalidOperation_ComputerName);
  78. }
  79. return name;
  80. }
  81. }
  82. private static readonly unsafe Lazy<OperatingSystem> s_osVersion = new Lazy<OperatingSystem>(() =>
  83. {
  84. var version = new Interop.Kernel32.OSVERSIONINFOEX { dwOSVersionInfoSize = sizeof(Interop.Kernel32.OSVERSIONINFOEX) };
  85. if (!Interop.Kernel32.GetVersionExW(ref version))
  86. {
  87. throw new InvalidOperationException(SR.InvalidOperation_GetVersion);
  88. }
  89. return new OperatingSystem(
  90. PlatformID.Win32NT,
  91. new Version(version.dwMajorVersion, version.dwMinorVersion, version.dwBuildNumber, (version.wServicePackMajor << 16) | version.wServicePackMinor),
  92. Marshal.PtrToStringUni((IntPtr)version.szCSDVersion));
  93. });
  94. public static string SystemDirectory
  95. {
  96. get
  97. {
  98. // Normally this will be C:\Windows\System32
  99. Span<char> initialBuffer = stackalloc char[32];
  100. var builder = new ValueStringBuilder(initialBuffer);
  101. uint length;
  102. while ((length = Interop.Kernel32.GetSystemDirectoryW(ref builder.GetPinnableReference(), (uint)builder.Capacity)) > builder.Capacity)
  103. {
  104. builder.EnsureCapacity((int)length);
  105. }
  106. if (length == 0)
  107. throw Win32Marshal.GetExceptionForLastWin32Error();
  108. builder.Length = (int)length;
  109. return builder.ToString();
  110. }
  111. }
  112. }
  113. }