Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Environment.cs
Go to the documentation of this file.
4using System.IO;
8using System.Text;
10using Internal.Win32;
11
12namespace System;
13
14public static class Environment
15{
16 public enum SpecialFolder
17 {
18 ApplicationData = 26,
21 Cookies = 33,
22 Desktop = 0,
23 Favorites = 6,
24 History = 34,
25 InternetCache = 32,
26 Programs = 2,
27 MyComputer = 17,
28 MyMusic = 13,
29 MyPictures = 39,
30 MyVideos = 14,
31 Recent = 8,
32 SendTo = 9,
33 StartMenu = 11,
34 Startup = 7,
35 System = 37,
36 Templates = 21,
38 Personal = 5,
39 MyDocuments = 5,
40 ProgramFiles = 38,
42 AdminTools = 48,
43 CDBurning = 59,
45 CommonDocuments = 46,
46 CommonMusic = 53,
47 CommonOemLinks = 58,
48 CommonPictures = 54,
49 CommonStartMenu = 22,
50 CommonPrograms = 23,
51 CommonStartup = 24,
53 CommonTemplates = 45,
54 CommonVideos = 55,
55 Fonts = 20,
58 UserProfile = 40,
60 ProgramFilesX86 = 42,
61 Resources = 56,
63 SystemX86 = 41,
64 Windows = 36
65 }
66
68 {
69 None = 0,
70 Create = 32768,
71 DoNotVerify = 16384
72 }
73
74 private static class WindowsVersion
75 {
76 internal static readonly bool IsWindows8OrAbove = GetIsWindows8OrAbove();
77
78 private unsafe static bool GetIsWindows8OrAbove()
79 {
80 ulong conditionMask = Interop.Kernel32.VerSetConditionMask(0uL, 2u, 3);
81 conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, 1u, 3);
82 conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, 32u, 3);
83 conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, 16u, 3);
85 lpVersionInfo.dwOSVersionInfoSize = sizeof(Interop.Kernel32.OSVERSIONINFOEX);
86 lpVersionInfo.dwMajorVersion = 6;
87 lpVersionInfo.dwMinorVersion = 2;
88 lpVersionInfo.wServicePackMajor = 0;
89 lpVersionInfo.wServicePackMinor = 0;
90 return Interop.Kernel32.VerifyVersionInfoW(ref lpVersionInfo, 51u, conditionMask);
91 }
92 }
93
94 private static string[] s_commandLineArgs;
95
96 private static volatile int s_processId;
97
98 private static volatile string s_processPath;
99
100 private static volatile OperatingSystem s_osVersion;
101
102 public static extern int CurrentManagedThreadId
103 {
104 [MethodImpl(MethodImplOptions.InternalCall)]
105 get;
106 }
107
108 public static extern int ExitCode
109 {
110 [MethodImpl(MethodImplOptions.InternalCall)]
111 get;
112 [MethodImpl(MethodImplOptions.InternalCall)]
113 set;
114 }
115
116 public static extern int TickCount
117 {
118 [MethodImpl(MethodImplOptions.InternalCall)]
119 get;
120 }
121
122 public static extern long TickCount64
123 {
124 [MethodImpl(MethodImplOptions.InternalCall)]
125 get;
126 }
127
128 public static int ProcessorCount { get; } = GetProcessorCount();
129
130
131 internal static bool IsSingleProcessor => ProcessorCount == 1;
132
133 public static bool HasShutdownStarted => false;
134
135 public static string CommandLine => PasteArguments.Paste(GetCommandLineArgs(), pasteFirstArgumentUsingArgV0Rules: true);
136
137 public static string CurrentDirectory
138 {
139 get
140 {
142 }
143 set
144 {
145 if (value == null)
146 {
147 throw new ArgumentNullException("value");
148 }
149 if (value.Length == 0)
150 {
151 throw new ArgumentException(SR.Argument_PathEmpty, "value");
152 }
154 }
155 }
156
157 public static int ProcessId
158 {
159 get
160 {
161 int num = s_processId;
162 if (num == 0)
163 {
165 num = s_processId;
166 }
167 return num;
168 }
169 }
170
171 public static string? ProcessPath
172 {
173 get
174 {
175 string text = s_processPath;
176 if (text == null)
177 {
180 }
181 if (text.Length == 0)
182 {
183 return null;
184 }
185 return text;
186 }
187 }
188
189 public static bool Is64BitProcess => IntPtr.Size == 8;
190
191 public static bool Is64BitOperatingSystem
192 {
193 get
194 {
195 if (!Is64BitProcess)
196 {
197 }
198 return true;
199 }
200 }
201
202 public static string NewLine => "\r\n";
203
205 {
206 get
207 {
208 OperatingSystem operatingSystem = s_osVersion;
209 if (operatingSystem == null)
210 {
212 operatingSystem = s_osVersion;
213 }
214 return operatingSystem;
215 }
216 }
217
218 public static Version Version
219 {
220 get
221 {
222 string text = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
223 ReadOnlySpan<char> readOnlySpan = text.AsSpan();
224 int num = readOnlySpan.IndexOfAny('-', '+', ' ');
225 if (num != -1)
226 {
227 readOnlySpan = readOnlySpan.Slice(0, num);
228 }
229 if (!System.Version.TryParse(readOnlySpan, out Version result))
230 {
231 return new Version();
232 }
233 return result;
234 }
235 }
236
237 public static string StackTrace
238 {
239 [MethodImpl(MethodImplOptions.NoInlining)]
240 get
241 {
242 return new StackTrace(fNeedFileInfo: true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal);
243 }
244 }
245
247
248 public static string UserName
249 {
250 get
251 {
252 Span<char> initialBuffer = stackalloc char[40];
253 ValueStringBuilder builder = new ValueStringBuilder(initialBuffer);
254 GetUserName(ref builder);
255 ReadOnlySpan<char> span = builder.AsSpan();
256 int num = span.IndexOf('\\');
257 if (num != -1)
258 {
259 span = span.Slice(num + 1);
260 }
261 string result = span.ToString();
262 builder.Dispose();
263 return result;
264 }
265 }
266
267 public static string UserDomainName
268 {
269 get
270 {
271 Span<char> initialBuffer = stackalloc char[40];
272 ValueStringBuilder builder = new ValueStringBuilder(initialBuffer);
273 GetUserName(ref builder);
274 ReadOnlySpan<char> span = builder.AsSpan();
275 int num = span.IndexOf('\\');
276 if (num != -1)
277 {
278 builder.Length = num;
279 return builder.ToString();
280 }
281 initialBuffer = stackalloc char[64];
282 ValueStringBuilder valueStringBuilder = new ValueStringBuilder(initialBuffer);
283 uint cchReferencedDomainName = (uint)valueStringBuilder.Capacity;
284 Span<byte> span2 = stackalloc byte[68];
285 uint cbSid = 68u;
286 uint peUse;
287 while (!Interop.Advapi32.LookupAccountNameW(null, ref builder.GetPinnableReference(), ref MemoryMarshal.GetReference(span2), ref cbSid, ref valueStringBuilder.GetPinnableReference(), ref cchReferencedDomainName, out peUse))
288 {
289 int lastPInvokeError = Marshal.GetLastPInvokeError();
290 if (lastPInvokeError != 122)
291 {
292 throw new InvalidOperationException(Win32Marshal.GetMessage(lastPInvokeError));
293 }
294 valueStringBuilder.EnsureCapacity((int)cchReferencedDomainName);
295 }
296 builder.Dispose();
297 valueStringBuilder.Length = (int)cchReferencedDomainName;
298 return valueStringBuilder.ToString();
299 }
300 }
301
302 private static string CurrentDirectoryCore
303 {
304 get
305 {
306 Span<char> initialBuffer = stackalloc char[260];
307 ValueStringBuilder outputBuilder = new ValueStringBuilder(initialBuffer);
308 uint currentDirectory;
309 while ((currentDirectory = Interop.Kernel32.GetCurrentDirectory((uint)outputBuilder.Capacity, ref outputBuilder.GetPinnableReference())) > outputBuilder.Capacity)
310 {
311 outputBuilder.EnsureCapacity((int)currentDirectory);
312 }
313 if (currentDirectory == 0)
314 {
316 }
317 outputBuilder.Length = (int)currentDirectory;
318 if (outputBuilder.AsSpan().Contains('~'))
319 {
320 string result = PathHelper.TryExpandShortFileName(ref outputBuilder, null);
321 outputBuilder.Dispose();
322 return result;
323 }
324 return outputBuilder.ToString();
325 }
326 set
327 {
329 {
330 int lastPInvokeError = Marshal.GetLastPInvokeError();
331 throw Win32Marshal.GetExceptionForWin32Error((lastPInvokeError == 2) ? 3 : lastPInvokeError, value);
332 }
333 }
334 }
335
336 public static int SystemPageSize
337 {
338 get
339 {
340 Interop.Kernel32.GetSystemInfo(out var lpSystemInfo);
341 return lpSystemInfo.dwPageSize;
342 }
343 }
344
346
347 public static string SystemDirectory
348 {
349 get
350 {
351 Span<char> initialBuffer = stackalloc char[32];
352 ValueStringBuilder valueStringBuilder = new ValueStringBuilder(initialBuffer);
353 uint systemDirectoryW;
354 while ((systemDirectoryW = Interop.Kernel32.GetSystemDirectoryW(ref valueStringBuilder.GetPinnableReference(), (uint)valueStringBuilder.Capacity)) > valueStringBuilder.Capacity)
355 {
356 valueStringBuilder.EnsureCapacity((int)systemDirectoryW);
357 }
358 if (systemDirectoryW == 0)
359 {
361 }
362 valueStringBuilder.Length = (int)systemDirectoryW;
363 return valueStringBuilder.ToString();
364 }
365 }
366
367 public unsafe static bool UserInteractive
368 {
369 get
370 {
371 IntPtr processWindowStation = Interop.User32.GetProcessWindowStation();
372 if (processWindowStation != IntPtr.Zero)
373 {
375 uint lpnLengthNeeded = 0u;
376 if (Interop.User32.GetUserObjectInformationW(processWindowStation, 1, &uSEROBJECTFLAGS, (uint)sizeof(Interop.User32.USEROBJECTFLAGS), ref lpnLengthNeeded))
377 {
378 return (uSEROBJECTFLAGS.dwFlags & 1) != 0;
379 }
380 }
381 return true;
382 }
383 }
384
385 public unsafe static long WorkingSet
386 {
387 get
388 {
390 ppsmemCounters.cb = (uint)sizeof(Interop.Kernel32.PROCESS_MEMORY_COUNTERS);
391 if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), ref ppsmemCounters, ppsmemCounters.cb))
392 {
393 return 0L;
394 }
395 return (long)(ulong)ppsmemCounters.WorkingSetSize;
396 }
397 }
398
399 [DllImport("QCall", CharSet = CharSet.Unicode)]
400 [DoesNotReturn]
401 private static extern void _Exit(int exitCode);
402
403 [DoesNotReturn]
404 public static void Exit(int exitCode)
405 {
406 _Exit(exitCode);
407 }
408
409 [MethodImpl(MethodImplOptions.InternalCall)]
410 [DoesNotReturn]
411 public static extern void FailFast(string? message);
412
413 [MethodImpl(MethodImplOptions.InternalCall)]
414 [DoesNotReturn]
415 public static extern void FailFast(string? message, Exception? exception);
416
417 [MethodImpl(MethodImplOptions.InternalCall)]
418 [DoesNotReturn]
419 public static extern void FailFast(string? message, Exception? exception, string? errorMessage);
420
421 [MethodImpl(MethodImplOptions.InternalCall)]
422 private static extern string[] GetCommandLineArgsNative();
423
424 public static string[] GetCommandLineArgs()
425 {
426 if (s_commandLineArgs == null)
427 {
429 }
430 return (string[])s_commandLineArgs.Clone();
431 }
432
433 [DllImport("QCall", CharSet = CharSet.Unicode)]
434 private static extern int GetProcessorCount();
435
436 internal static string GetResourceStringLocal(string key)
437 {
438 return SR.GetResourceString(key);
439 }
440
441 public static string? GetEnvironmentVariable(string variable)
442 {
443 if (variable == null)
444 {
445 throw new ArgumentNullException("variable");
446 }
447 return GetEnvironmentVariableCore(variable);
448 }
449
450 public static string? GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
451 {
452 if (target == EnvironmentVariableTarget.Process)
453 {
454 return GetEnvironmentVariable(variable);
455 }
456 if (variable == null)
457 {
458 throw new ArgumentNullException("variable");
459 }
460 bool fromMachine = ValidateAndConvertRegistryTarget(target);
461 return GetEnvironmentVariableFromRegistry(variable, fromMachine);
462 }
463
465 {
466 if (target == EnvironmentVariableTarget.Process)
467 {
469 }
470 bool fromMachine = ValidateAndConvertRegistryTarget(target);
471 return GetEnvironmentVariablesFromRegistry(fromMachine);
472 }
473
474 public static void SetEnvironmentVariable(string variable, string? value)
475 {
476 ValidateVariableAndValue(variable, ref value);
478 }
479
480 public static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
481 {
482 if (target == EnvironmentVariableTarget.Process)
483 {
484 SetEnvironmentVariable(variable, value);
485 return;
486 }
487 ValidateVariableAndValue(variable, ref value);
488 bool fromMachine = ValidateAndConvertRegistryTarget(target);
489 SetEnvironmentVariableFromRegistry(variable, value, fromMachine);
490 }
491
492 public static string ExpandEnvironmentVariables(string name)
493 {
494 if (name == null)
495 {
496 throw new ArgumentNullException("name");
497 }
498 if (name.Length == 0)
499 {
500 return name;
501 }
503 }
504
505 internal static void SetCommandLineArgs(string[] cmdLineArgs)
506 {
507 s_commandLineArgs = cmdLineArgs;
508 }
509
510 public static string GetFolderPath(SpecialFolder folder)
511 {
512 return GetFolderPath(folder, SpecialFolderOption.None);
513 }
514
515 public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
516 {
517 if (!Enum.IsDefined(typeof(SpecialFolder), folder))
518 {
519 throw new ArgumentOutOfRangeException("folder", folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
520 }
521 if (option != 0 && !Enum.IsDefined(typeof(SpecialFolderOption), option))
522 {
523 throw new ArgumentOutOfRangeException("option", option, SR.Format(SR.Arg_EnumIllegalVal, option));
524 }
525 return GetFolderPathCore(folder, option);
526 }
527
529 {
530 return target switch
531 {
532 EnvironmentVariableTarget.Machine => true,
533 EnvironmentVariableTarget.User => false,
534 _ => throw new ArgumentOutOfRangeException("target", target, SR.Format(SR.Arg_EnumIllegalVal, target)),
535 };
536 }
537
538 private static void ValidateVariableAndValue(string variable, ref string value)
539 {
540 if (variable == null)
541 {
542 throw new ArgumentNullException("variable");
543 }
544 if (variable.Length == 0)
545 {
546 throw new ArgumentException(SR.Argument_StringZeroLength, "variable");
547 }
548 if (variable[0] == '\0')
549 {
551 }
552 if (variable.Contains('='))
553 {
554 throw new ArgumentException(SR.Argument_IllegalEnvVarName, "variable");
555 }
556 if (string.IsNullOrEmpty(value) || value[0] == '\0')
557 {
558 value = null;
559 }
560 }
561
562 private static string GetEnvironmentVariableFromRegistry(string variable, bool fromMachine)
563 {
564 using RegistryKey registryKey = OpenEnvironmentKeyIfExists(fromMachine, writable: false);
565 return registryKey?.GetValue(variable) as string;
566 }
567
568 private unsafe static void SetEnvironmentVariableFromRegistry(string variable, string value, bool fromMachine)
569 {
570 if (!fromMachine && variable.Length >= 255)
571 {
572 throw new ArgumentException(SR.Argument_LongEnvVarValue, "variable");
573 }
574 using (RegistryKey registryKey = OpenEnvironmentKeyIfExists(fromMachine, writable: true))
575 {
576 if (registryKey != null)
577 {
578 if (value == null)
579 {
580 registryKey.DeleteValue(variable, throwOnMissingValue: false);
581 }
582 else
583 {
584 registryKey.SetValue(variable, value);
585 }
586 }
587 }
588 fixed (char* ptr = &"Environment".GetPinnableReference())
589 {
590 IntPtr pdwResult;
591 IntPtr intPtr = Interop.User32.SendMessageTimeout(new IntPtr(65535), 26, IntPtr.Zero, (IntPtr)ptr, 0, 1000, out pdwResult);
592 }
593 }
594
595 private static IDictionary GetEnvironmentVariablesFromRegistry(bool fromMachine)
596 {
597 Hashtable hashtable = new Hashtable();
598 using (RegistryKey registryKey = OpenEnvironmentKeyIfExists(fromMachine, writable: false))
599 {
600 if (registryKey != null)
601 {
602 string[] valueNames = registryKey.GetValueNames();
603 foreach (string text in valueNames)
604 {
605 string value = registryKey.GetValue(text, "").ToString();
606 try
607 {
608 hashtable.Add(text, value);
609 }
610 catch (ArgumentException)
611 {
612 }
613 }
614 }
615 }
616 return hashtable;
617 }
618
619 private static RegistryKey OpenEnvironmentKeyIfExists(bool fromMachine, bool writable)
620 {
621 RegistryKey registryKey;
622 string name;
623 if (fromMachine)
624 {
625 registryKey = Registry.LocalMachine;
626 name = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
627 }
628 else
629 {
630 registryKey = Registry.CurrentUser;
631 name = "Environment";
632 }
633 return registryKey.OpenSubKey(name, writable);
634 }
635
636 private static void GetUserName(ref ValueStringBuilder builder)
637 {
638 uint lpnSize = 0u;
639 while (Interop.Secur32.GetUserNameExW(2, ref builder.GetPinnableReference(), ref lpnSize) == Interop.BOOLEAN.FALSE)
640 {
641 if (Marshal.GetLastPInvokeError() == 234)
642 {
643 builder.EnsureCapacity(checked((int)lpnSize));
644 continue;
645 }
646 builder.Length = 0;
647 return;
648 }
649 builder.Length = (int)lpnSize;
650 }
651
652 private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
653 {
654 string folderGuid;
655 switch (folder)
656 {
657 case SpecialFolder.ApplicationData:
658 folderGuid = "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}";
659 break;
660 case SpecialFolder.CommonApplicationData:
661 folderGuid = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}";
662 break;
663 case SpecialFolder.LocalApplicationData:
664 folderGuid = "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}";
665 break;
666 case SpecialFolder.Cookies:
667 folderGuid = "{2B0F765D-C0E9-4171-908E-08A611B84FF6}";
668 break;
669 case SpecialFolder.Desktop:
670 folderGuid = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}";
671 break;
672 case SpecialFolder.Favorites:
673 folderGuid = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}";
674 break;
675 case SpecialFolder.History:
676 folderGuid = "{D9DC8A3B-B784-432E-A781-5A1130A75963}";
677 break;
678 case SpecialFolder.InternetCache:
679 folderGuid = "{352481E8-33BE-4251-BA85-6007CAEDCF9D}";
680 break;
681 case SpecialFolder.Programs:
682 folderGuid = "{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}";
683 break;
684 case SpecialFolder.MyComputer:
685 folderGuid = "{0AC0837C-BBF8-452A-850D-79D08E667CA7}";
686 break;
687 case SpecialFolder.MyMusic:
688 folderGuid = "{4BD8D571-6D19-48D3-BE97-422220080E43}";
689 break;
690 case SpecialFolder.MyPictures:
691 folderGuid = "{33E28130-4E1E-4676-835A-98395C3BC3BB}";
692 break;
693 case SpecialFolder.MyVideos:
694 folderGuid = "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}";
695 break;
696 case SpecialFolder.Recent:
697 folderGuid = "{AE50C081-EBD2-438A-8655-8A092E34987A}";
698 break;
699 case SpecialFolder.SendTo:
700 folderGuid = "{8983036C-27C0-404B-8F08-102D10DCFD74}";
701 break;
702 case SpecialFolder.StartMenu:
703 folderGuid = "{625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}";
704 break;
705 case SpecialFolder.Startup:
706 folderGuid = "{B97D20BB-F46A-4C97-BA10-5E3608430854}";
707 break;
708 case SpecialFolder.System:
709 folderGuid = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}";
710 break;
711 case SpecialFolder.Templates:
712 folderGuid = "{A63293E8-664E-48DB-A079-DF759E0509F7}";
713 break;
714 case SpecialFolder.DesktopDirectory:
715 folderGuid = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}";
716 break;
717 case SpecialFolder.Personal:
718 folderGuid = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}";
719 break;
720 case SpecialFolder.ProgramFiles:
721 folderGuid = "{905e63b6-c1bf-494e-b29c-65b732d3d21a}";
722 break;
723 case SpecialFolder.CommonProgramFiles:
724 folderGuid = "{F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}";
725 break;
726 case SpecialFolder.AdminTools:
727 folderGuid = "{724EF170-A42D-4FEF-9F26-B60E846FBA4F}";
728 break;
729 case SpecialFolder.CDBurning:
730 folderGuid = "{9E52AB10-F80D-49DF-ACB8-4330F5687855}";
731 break;
732 case SpecialFolder.CommonAdminTools:
733 folderGuid = "{D0384E7D-BAC3-4797-8F14-CBA229B392B5}";
734 break;
735 case SpecialFolder.CommonDocuments:
736 folderGuid = "{ED4824AF-DCE4-45A8-81E2-FC7965083634}";
737 break;
738 case SpecialFolder.CommonMusic:
739 folderGuid = "{3214FAB5-9757-4298-BB61-92A9DEAA44FF}";
740 break;
741 case SpecialFolder.CommonOemLinks:
742 folderGuid = "{C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}";
743 break;
744 case SpecialFolder.CommonPictures:
745 folderGuid = "{B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}";
746 break;
747 case SpecialFolder.CommonStartMenu:
748 folderGuid = "{A4115719-D62E-491D-AA7C-E74B8BE3B067}";
749 break;
750 case SpecialFolder.CommonPrograms:
751 folderGuid = "{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}";
752 break;
753 case SpecialFolder.CommonStartup:
754 folderGuid = "{82A5EA35-D9CD-47C5-9629-E15D2F714E6E}";
755 break;
756 case SpecialFolder.CommonDesktopDirectory:
757 folderGuid = "{C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}";
758 break;
759 case SpecialFolder.CommonTemplates:
760 folderGuid = "{B94237E7-57AC-4347-9151-B08C6C32D1F7}";
761 break;
762 case SpecialFolder.CommonVideos:
763 folderGuid = "{2400183A-6185-49FB-A2D8-4A392A602BA3}";
764 break;
765 case SpecialFolder.Fonts:
766 folderGuid = "{FD228CB7-AE11-4AE3-864C-16F3910AB8FE}";
767 break;
768 case SpecialFolder.NetworkShortcuts:
769 folderGuid = "{C5ABBF53-E17F-4121-8900-86626FC2C973}";
770 break;
771 case SpecialFolder.PrinterShortcuts:
772 folderGuid = "{76FC4E2D-D6AD-4519-A663-37BD56068185}";
773 break;
774 case SpecialFolder.UserProfile:
775 folderGuid = "{5E6C858F-0E22-4760-9AFE-EA3317B67173}";
776 break;
777 case SpecialFolder.CommonProgramFilesX86:
778 folderGuid = "{DE974D24-D9C6-4D3E-BF91-F4455120B917}";
779 break;
780 case SpecialFolder.ProgramFilesX86:
781 folderGuid = "{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}";
782 break;
783 case SpecialFolder.Resources:
784 folderGuid = "{8AD10C31-2ADB-4296-A8F7-E4701232C972}";
785 break;
786 case SpecialFolder.LocalizedResources:
787 folderGuid = "{2A00375E-224C-49DE-B8D1-440DF7EF3DDC}";
788 break;
789 case SpecialFolder.SystemX86:
790 folderGuid = "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}";
791 break;
792 case SpecialFolder.Windows:
793 folderGuid = "{F38BF404-1D43-42F2-9305-67DE0B28FC23}";
794 break;
795 default:
796 return string.Empty;
797 }
798 return GetKnownFolderPath(folderGuid, option);
799 }
800
801 private static string GetKnownFolderPath(string folderGuid, SpecialFolderOption option)
802 {
803 Guid rfid = new Guid(folderGuid);
804 if (Interop.Shell32.SHGetKnownFolderPath(rfid, (uint)option, IntPtr.Zero, out var ppszPath) != 0)
805 {
806 return string.Empty;
807 }
808 return ppszPath;
809 }
810
811 public static string[] GetLogicalDrives()
812 {
814 }
815
816 private static string ExpandEnvironmentVariablesCore(string name)
817 {
818 Span<char> initialBuffer = stackalloc char[128];
819 ValueStringBuilder valueStringBuilder = new ValueStringBuilder(initialBuffer);
820 uint num;
821 while ((num = Interop.Kernel32.ExpandEnvironmentStrings(name, ref valueStringBuilder.GetPinnableReference(), (uint)valueStringBuilder.Capacity)) > valueStringBuilder.Capacity)
822 {
823 valueStringBuilder.EnsureCapacity((int)num);
824 }
825 if (num == 0)
826 {
828 }
829 valueStringBuilder.Length = (int)(num - 1);
830 return valueStringBuilder.ToString();
831 }
832
833 [MethodImpl(MethodImplOptions.NoInlining)]
834 private static int GetProcessId()
835 {
837 }
838
839 private static string GetProcessPath()
840 {
841 Span<char> initialBuffer = stackalloc char[260];
842 ValueStringBuilder valueStringBuilder = new ValueStringBuilder(initialBuffer);
843 uint moduleFileName;
844 while ((moduleFileName = Interop.Kernel32.GetModuleFileName(IntPtr.Zero, ref valueStringBuilder.GetPinnableReference(), (uint)valueStringBuilder.Capacity)) >= valueStringBuilder.Capacity)
845 {
846 valueStringBuilder.EnsureCapacity((int)moduleFileName);
847 }
848 if (moduleFileName == 0)
849 {
851 }
852 valueStringBuilder.Length = (int)moduleFileName;
853 return valueStringBuilder.ToString();
854 }
855
856 private unsafe static OperatingSystem GetOSVersion()
857 {
858 if (Interop.NtDll.RtlGetVersionEx(out var osvi) != 0)
859 {
861 }
862 Version version = new Version((int)osvi.dwMajorVersion, (int)osvi.dwMinorVersion, (int)osvi.dwBuildNumber, 0);
863 if (osvi.szCSDVersion[0] == '\0')
864 {
865 return new OperatingSystem(PlatformID.Win32NT, version);
866 }
867 return new OperatingSystem(PlatformID.Win32NT, version, new string(osvi.szCSDVersion));
868 }
869
870 private static string GetEnvironmentVariableCore(string variable)
871 {
872 Span<char> initialBuffer = stackalloc char[128];
873 ValueStringBuilder valueStringBuilder = new ValueStringBuilder(initialBuffer);
874 uint environmentVariable;
875 while ((environmentVariable = Interop.Kernel32.GetEnvironmentVariable(variable, ref valueStringBuilder.GetPinnableReference(), (uint)valueStringBuilder.Capacity)) > valueStringBuilder.Capacity)
876 {
877 valueStringBuilder.EnsureCapacity((int)environmentVariable);
878 }
879 if (environmentVariable == 0 && Marshal.GetLastPInvokeError() == 203)
880 {
881 valueStringBuilder.Dispose();
882 return null;
883 }
884 valueStringBuilder.Length = (int)environmentVariable;
885 return valueStringBuilder.ToString();
886 }
887
888 private static void SetEnvironmentVariableCore(string variable, string value)
889 {
891 {
892 int lastPInvokeError = Marshal.GetLastPInvokeError();
893 switch (lastPInvokeError)
894 {
895 case 203:
896 break;
897 case 206:
899 case 8:
900 case 1450:
901 throw new OutOfMemoryException(Interop.Kernel32.GetMessage(lastPInvokeError));
902 default:
903 throw new ArgumentException(Interop.Kernel32.GetMessage(lastPInvokeError));
904 }
905 }
906 }
907
909 {
910 char* environmentStringsW = Interop.Kernel32.GetEnvironmentStringsW();
911 if (environmentStringsW == null)
912 {
913 throw new OutOfMemoryException();
914 }
915 try
916 {
917 Hashtable hashtable = new Hashtable();
918 char* ptr = environmentStringsW;
919 while (true)
920 {
922 if (span.IsEmpty)
923 {
924 break;
925 }
926 int num = span.IndexOf('=');
927 if (num > 0)
928 {
929 string key = new string(span.Slice(0, num));
930 string value = new string(span.Slice(num + 1));
931 try
932 {
933 hashtable.Add(key, value);
934 }
935 catch (ArgumentException)
936 {
937 }
938 }
939 ptr += span.Length + 1;
940 }
941 return hashtable;
942 }
943 finally
944 {
945 Interop.BOOL bOOL = Interop.Kernel32.FreeEnvironmentStringsW(environmentStringsW);
946 }
947 }
948}
object GetValue(string name)
void DeleteValue(string name, bool throwOnMissingValue)
void SetValue(string name, string value)
RegistryKey OpenSubKey(string name)
static readonly RegistryKey LocalMachine
Definition Registry.cs:9
static readonly RegistryKey CurrentUser
Definition Registry.cs:7
static bool LookupAccountNameW(string lpSystemName, ref char lpAccountName, ref byte Sid, ref uint cbSid, ref char ReferencedDomainName, ref uint cchReferencedDomainName, out uint peUse)
static bool SetEnvironmentVariable(string lpName, string lpValue)
static ulong VerSetConditionMask(ulong ConditionMask, uint TypeMask, byte Condition)
static bool SetCurrentDirectory(string path)
static uint GetEnvironmentVariable(string lpName, ref char lpBuffer, uint nSize)
static bool GetProcessMemoryInfo(IntPtr Process, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb)
static uint GetSystemDirectoryW(ref char lpBuffer, uint uSize)
static void GetSystemInfo(out SYSTEM_INFO lpSystemInfo)
static uint GetCurrentProcessId()
static int GetComputerName(ref char lpBuffer, ref uint nSize)
static uint ExpandEnvironmentStrings(string lpSrc, ref char lpDst, uint nSize)
static string GetMessage(int errorCode)
Definition Interop.cs:113
static unsafe char * GetEnvironmentStringsW()
static uint GetModuleFileName(IntPtr hModule, ref char lpFilename, uint nSize)
static unsafe BOOL FreeEnvironmentStringsW(char *lpszEnvironmentBlock)
static bool VerifyVersionInfoW(ref OSVERSIONINFOEX lpVersionInfo, uint dwTypeMask, ulong dwlConditionMask)
static uint GetCurrentDirectory(uint nBufferLength, ref char lpBuffer)
static IntPtr GetCurrentProcess()
static unsafe int RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi)
Definition Interop.cs:1681
static BOOLEAN GetUserNameExW(int NameFormat, ref char lpNameBuffer, ref uint lpnSize)
static int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out string ppszPath)
static IntPtr SendMessageTimeout(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, int flags, int timeout, out IntPtr pdwResult)
static IntPtr GetProcessWindowStation()
static unsafe bool GetUserObjectInformationW(IntPtr hObj, int nIndex, void *pvBuffer, uint nLength, ref uint lpnLengthNeeded)
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static bool IsDefined(Type enumType, object value)
Definition Enum.cs:359
static readonly bool IsWindows8OrAbove
static unsafe bool GetIsWindows8OrAbove()
static string[] GetLogicalDrives()
static string[] s_commandLineArgs
static volatile OperatingSystem s_osVersion
static string SystemDirectory
static string NewLine
static Version Version
static int SystemPageSize
static void _Exit(int exitCode)
static long TickCount64
static void FailFast(string? message)
static volatile int s_processId
static string GetEnvironmentVariableCore(string variable)
static int GetProcessId()
static string StackTrace
static string CurrentDirectory
static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
static string CurrentDirectoryCore
static IDictionary GetEnvironmentVariablesFromRegistry(bool fromMachine)
static int ExitCode
static string UserDomainName
static string GetEnvironmentVariableFromRegistry(string variable, bool fromMachine)
static void SetCommandLineArgs(string[] cmdLineArgs)
static unsafe void SetEnvironmentVariableFromRegistry(string variable, string value, bool fromMachine)
static string GetProcessPath()
static int GetProcessorCount()
static void GetUserName(ref ValueStringBuilder builder)
static string GetResourceStringLocal(string key)
static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
static bool HasShutdownStarted
static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
static void ValidateVariableAndValue(string variable, ref string value)
static unsafe bool UserInteractive
static unsafe IDictionary GetEnvironmentVariables()
static int ProcessorCount
static unsafe long WorkingSet
static string ExpandEnvironmentVariables(string name)
static bool IsSingleProcessor
static string CommandLine
static string[] GetCommandLineArgsNative()
static int CurrentManagedThreadId
static volatile string s_processPath
static bool Is64BitOperatingSystem
static ? string GetEnvironmentVariable(string variable)
static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
static string UserName
static int ProcessId
static string GetFolderPath(SpecialFolder folder)
static void SetEnvironmentVariable(string variable, string? value)
static bool Is64BitProcess
static RegistryKey OpenEnvironmentKeyIfExists(bool fromMachine, bool writable)
static string[] GetCommandLineArgs()
static void FailFast(string? message, Exception? exception, string? errorMessage)
static unsafe OperatingSystem GetOSVersion()
static string ExpandEnvironmentVariablesCore(string name)
static void SetEnvironmentVariableCore(string variable, string value)
static int TickCount
static string GetKnownFolderPath(string folderGuid, SpecialFolderOption option)
static void FailFast(string? message, Exception? exception)
static ? string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
static void Exit(int exitCode)
static ? string ProcessPath
static bool ValidateAndConvertRegistryTarget(EnvironmentVariableTarget target)
static OperatingSystem OSVersion
static string MachineName
static bool IsWindows8OrAbove
static string[] GetLogicalDrives()
static string TryExpandShortFileName(ref ValueStringBuilder outputBuilder, string originalPath)
Definition PathHelper.cs:64
static Exception GetExceptionForLastWin32Error(string path="")
static string GetMessage(int errorCode)
static Exception GetExceptionForWin32Error(int errorCode, string path="")
static string Paste(IEnumerable< string > arguments, bool pasteFirstArgumentUsingArgV0Rules)
static unsafe ReadOnlySpan< char > CreateReadOnlySpanFromNullTerminated(char *value)
static string InvalidOperation_ComputerName
Definition SR.cs:1428
static string Arg_EnumIllegalVal
Definition SR.cs:144
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_StringZeroLength
Definition SR.cs:882
static string Argument_PathEmpty
Definition SR.cs:840
static string Argument_IllegalEnvVarName
Definition SR.cs:628
static string GetResourceString(string resourceKey)
Definition SR.cs:101
static string Argument_StringFirstCharIsZero
Definition SR.cs:880
static string Argument_LongEnvVarValue
Definition SR.cs:758
static string InvalidOperation_GetVersion
Definition SR.cs:1450
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out Version? result)
Definition Version.cs:324
static readonly IntPtr Zero
Definition IntPtr.cs:18
ReadOnlySpan< T > Slice(int start)
override string ToString()
ReadOnlySpan< char > AsSpan(bool terminate)