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