Environment.Windows.cs 4.6 KB

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