Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Microsoft.Win32;
  7.  
  8. namespace MyNamespace
  9. {
  10. internal class WindowsRegistry
  11. {
  12. /// <summary>
  13. /// Reads a Windows registry value. This method does nothing if not running on Windows.
  14. /// </summary>
  15. /// <param name="root">The registry hive: "HKLM" or "HKCU".</param>
  16. /// <param name="key">The path of the key to read the value from.</param>
  17. /// <param name="value">The value to read.</param>
  18. /// <returns>The read value if it exists and is a string; otherwise, null.</returns>
  19. public string GetStringValue(string root, string key, string value)
  20. {
  21. #if NETFULL
  22. RegistryKey rootKey;
  23. switch (root)
  24. {
  25. case "HKLM":
  26. rootKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
  27. break;
  28. case "HKCU":
  29. rootKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
  30. break;
  31. default:
  32. throw new InvalidOperationException($"Registry root key {root} unknown.");
  33. }
  34. var regKey = rootKey.OpenSubKey(key);
  35. if (regKey != null)
  36. {
  37. object obj = regKey.GetValue(value);
  38. regKey.Close();
  39. if (obj is string)
  40. {
  41. return (string)obj;
  42. }
  43. }
  44. #else
  45. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  46. {
  47. UIntPtr rootKey;
  48. switch (root)
  49. {
  50. case "HKLM":
  51. rootKey = HKEY_LOCAL_MACHINE;
  52. break;
  53. case "HKCU":
  54. rootKey = HKEY_CURRENT_USER;
  55. break;
  56. default:
  57. throw new InvalidOperationException($"Registry root key {root} unknown.");
  58. }
  59. UIntPtr keyHandle;
  60. if (RegOpenKeyEx(rootKey, key, 0, KEY_READ | KEY_WOW64_64KEY, out keyHandle) == 0)
  61. {
  62. RegistryValueKind kind;
  63. var sb = new StringBuilder(1000);
  64. int length = sb.Capacity;
  65. bool found = RegQueryValueEx(keyHandle, value, 0, out kind, sb, ref length) == 0;
  66. RegCloseKey(keyHandle);
  67. if (found)
  68. return sb.ToString();
  69. }
  70. }
  71. #endif
  72. return null;
  73. }
  74.  
  75. #region Registry interop
  76.  
  77. #if !NETFULL
  78.  
  79. [DllImport("advapi32.dll")]
  80. private static extern int RegOpenKeyEx(
  81. UIntPtr hKey,
  82. string subKey,
  83. int ulOptions,
  84. int samDesired,
  85. out UIntPtr hkResult);
  86.  
  87. [DllImport("advapi32.dll", SetLastError = true)]
  88. private static extern int RegCloseKey(
  89. UIntPtr hKey);
  90.  
  91. [DllImport("advapi32.dll", SetLastError = true)]
  92. private static extern uint RegQueryValueEx(
  93. UIntPtr hKey,
  94. string lpValueName,
  95. int lpReserved,
  96. out RegistryValueKind lpType,
  97. IntPtr lpData,
  98. ref int lpcbData);
  99.  
  100. [DllImport("advapi32.dll", SetLastError = true)]
  101. private static extern uint RegQueryValueEx(
  102. UIntPtr hKey,
  103. string lpValueName,
  104. int lpReserved,
  105. out RegistryValueKind lpType,
  106. StringBuilder lpData,
  107. ref int lpcbData);
  108.  
  109. private static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
  110. private static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
  111.  
  112. private const int KEY_READ = 0x20019;
  113. private const int KEY_WOW64_64KEY = 0x0100;
  114.  
  115. private enum RegistryValueKind
  116. {
  117. None = 0,
  118. String = 1,
  119. ExpandString = 2,
  120. Binary = 3,
  121. DWord = 4,
  122. DWordBE = 5,
  123. Link = 6,
  124. MultiString = 7
  125. }
  126.  
  127. #endif
  128.  
  129. #endregion Registry interop
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement