This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Environment.cs
194 lines (158 loc) · 7.7 KB
/
Environment.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Internal.Runtime.Augments;
namespace System
{
public static partial class Environment
{
public static string GetEnvironmentVariable(string variable)
{
return EnvironmentAugments.GetEnvironmentVariable(variable);
}
public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
{
return EnvironmentAugments.GetEnvironmentVariable(variable, target);
}
public static IDictionary GetEnvironmentVariables()
{
// To maintain complete compatibility with prior versions we need to return a Hashtable.
// We did ship a prior version of Core with LowLevelDictionary, which does iterate the
// same (e.g. yields DictionaryEntry), but it is not a public type.
//
// While we could pass Hashtable back from CoreCLR the type is also defined here. We only
// want to surface the local Hashtable.
return EnvironmentAugments.EnumerateEnvironmentVariables().ToHashtable();
}
public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
{
// See comments in GetEnvironmentVariables()
return EnvironmentAugments.EnumerateEnvironmentVariables(target).ToHashtable();
}
private static Hashtable ToHashtable(this IEnumerable<KeyValuePair<string, string>> pairs)
{
Hashtable hashTable = new Hashtable();
foreach (KeyValuePair<string, string> pair in pairs)
{
hashTable.Add(pair.Key, pair.Value);
}
return hashTable;
}
public static void SetEnvironmentVariable(string variable, string value)
{
EnvironmentAugments.SetEnvironmentVariable(variable, value);
}
public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target)
{
EnvironmentAugments.SetEnvironmentVariable(variable, value, target);
}
public static string CommandLine
{
get
{
return PasteArguments.Paste(GetCommandLineArgs(), pasteFirstArgumentUsingArgV0Rules: true);
}
}
public static string CurrentDirectory
{
get { return CurrentDirectoryCore; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (value.Length == 0)
{
throw new ArgumentException(SR.Argument_PathEmpty, nameof(value));
}
CurrentDirectoryCore = value;
}
}
public static int CurrentManagedThreadId => EnvironmentAugments.CurrentManagedThreadId;
public static void Exit(int exitCode) => EnvironmentAugments.Exit(exitCode);
public static void FailFast(string message) => FailFast(message, exception: null);
public static void FailFast(string message, Exception exception) => EnvironmentAugments.FailFast(message, exception);
public static string ExpandEnvironmentVariables(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length == 0)
{
return name;
}
return ExpandEnvironmentVariablesCore(name);
}
public static string[] GetCommandLineArgs() => EnvironmentAugments.GetCommandLineArgs();
public static string GetFolderPath(SpecialFolder folder) => GetFolderPath(folder, SpecialFolderOption.None);
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
if (!Enum.IsDefined(typeof(SpecialFolder), folder))
{
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
}
if (option != SpecialFolderOption.None && !Enum.IsDefined(typeof(SpecialFolderOption), option))
{
throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
}
return GetFolderPathCore(folder, option);
}
public static bool HasShutdownStarted => EnvironmentAugments.HasShutdownStarted;
public static bool Is64BitProcess => IntPtr.Size == 8;
public static bool Is64BitOperatingSystem => Is64BitProcess || Is64BitOperatingSystemWhen32BitProcess;
public static OperatingSystem OSVersion => s_osVersion.Value;
public static int ProcessorCount => EnvironmentAugments.ProcessorCount;
public static string StackTrace
{
[MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts
get
{
return EnvironmentAugments.StackTrace;
}
}
public static int TickCount => EnvironmentAugments.TickCount;
public static bool UserInteractive => true;
public static Version Version
{
// Previously this represented the File version of mscorlib.dll. Many other libraries in the framework and outside took dependencies on the first three parts of this version
// remaining constant throughout 4.x. From 4.0 to 4.5.2 this was fine since the file version only incremented the last part. Starting with 4.6 we switched to a file versioning
// scheme that matched the product version. In order to preserve compatibility with existing libraries, this needs to be hard-coded.
get { return new Version(4, 0, 30319, 42000); }
}
public static long WorkingSet
{
get
{
// Use reflection to access the implementation in System.Diagnostics.Process.dll. While far from ideal,
// we do this to avoid duplicating the Windows, Linux, macOS, and potentially other platform-specific implementations
// present in Process. If it proves important, we could look at separating that functionality out of Process into
// Common files which could also be included here.
Type processType = Type.GetType("System.Diagnostics.Process, System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false);
IDisposable currentProcess = processType?.GetTypeInfo().GetDeclaredMethod("GetCurrentProcess")?.Invoke(null, null) as IDisposable;
if (currentProcess != null)
{
try
{
object result = processType.GetTypeInfo().GetDeclaredProperty("WorkingSet64")?.GetMethod?.Invoke(currentProcess, null);
if (result is long) return (long)result;
}
catch (TargetInvocationException tie)
{
if(tie.InnerException != null)
throw tie.InnerException;
throw tie;
}
finally { currentProcess.Dispose(); }
}
// Could not get the current working set.
return 0;
}
}
}
}