Environment.Windows.cs 4.7 KB

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