OS.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SmartCoalApplication.SystemLayer
  9. {
  10. public static class OS
  11. {
  12. /// <summary>
  13. /// Gets a flag indicating whether we are running on Windows Vista or later.
  14. /// </summary>
  15. /// <remarks>
  16. /// This is the preferred method for detecting this condition, and the most performant
  17. /// (it avoids creating some temporary Version objects).
  18. /// </remarks>
  19. public static bool IsVistaOrLater
  20. {
  21. get
  22. {
  23. return Environment.OSVersion.Version.Major >= 6;
  24. }
  25. }
  26. public static Version WindowsXP
  27. {
  28. get
  29. {
  30. return new Version(5, 1);
  31. }
  32. }
  33. public static Version WindowsServer2003
  34. {
  35. get
  36. {
  37. return new Version(5, 2);
  38. }
  39. }
  40. public static Version WindowsVista
  41. {
  42. get
  43. {
  44. return new Version(6, 0);
  45. }
  46. }
  47. public static string Revision
  48. {
  49. get
  50. {
  51. NativeStructs.OSVERSIONINFOEX osviex = new NativeStructs.OSVERSIONINFOEX();
  52. osviex.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
  53. bool result = SafeNativeMethods.GetVersionEx(ref osviex);
  54. if (result)
  55. {
  56. return osviex.szCSDVersion;
  57. }
  58. else
  59. {
  60. return "Unknown";
  61. }
  62. }
  63. }
  64. public static OSType Type
  65. {
  66. get
  67. {
  68. NativeStructs.OSVERSIONINFOEX osviex = new NativeStructs.OSVERSIONINFOEX();
  69. osviex.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
  70. bool result = SafeNativeMethods.GetVersionEx(ref osviex);
  71. OSType type;
  72. if (result)
  73. {
  74. if (Enum.IsDefined(typeof(OSType), (OSType)osviex.wProductType))
  75. {
  76. type = (OSType)osviex.wProductType;
  77. }
  78. else
  79. {
  80. type = OSType.Unknown;
  81. }
  82. }
  83. else
  84. {
  85. type = OSType.Unknown;
  86. }
  87. return type;
  88. }
  89. }
  90. public static bool IsDotNetVersionInstalled(int major, int minor, int servicePack, bool allowClientSubset)
  91. {
  92. bool result = false;
  93. if (!result)
  94. {
  95. // .NET 2.0 should always have a build # of 50727
  96. result |= IsDotNet2VersionInstalled(major, minor, 50727, servicePack);
  97. }
  98. if (!result)
  99. {
  100. result |= IsDotNet3VersionInstalled(major, minor, servicePack);
  101. }
  102. if (!result && allowClientSubset)
  103. {
  104. result |= IsDotNet3ClientVersionInstalled(major, minor, servicePack);
  105. }
  106. return result;
  107. }
  108. private static bool IsDotNet2VersionInstalled(int major, int minor, int build, int minServicePack)
  109. {
  110. bool result = false;
  111. const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}.{2}";
  112. string regKeyName = string.Format(regKeyNameFormat, major.ToString(CultureInfo.InvariantCulture),
  113. minor.ToString(CultureInfo.InvariantCulture), build.ToString(CultureInfo.InvariantCulture));
  114. if (!result)
  115. {
  116. const string regValueName = "Install";
  117. result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
  118. }
  119. if (result)
  120. {
  121. const string regValueName = "SP";
  122. int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
  123. result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
  124. }
  125. return result;
  126. }
  127. private static bool IsDotNet3VersionInstalled(int major, int minor, int minServicePack)
  128. {
  129. bool result = false;
  130. const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}";
  131. string regKeyName = string.Format(regKeyNameFormat, major, minor);
  132. if (!result)
  133. {
  134. const string regValueName = "Install";
  135. result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
  136. }
  137. if (result)
  138. {
  139. const string regValueName = "SP";
  140. int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
  141. result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
  142. }
  143. return result;
  144. }
  145. private static bool IsDotNet3ClientVersionInstalled(int major, int minor, int minServicePack)
  146. {
  147. bool result = false;
  148. const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\DotNetClient\\v{0}.{1}";
  149. string regKeyName = string.Format(regKeyNameFormat, major, minor);
  150. if (!result)
  151. {
  152. const string regValueName = "Install";
  153. result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
  154. }
  155. if (result)
  156. {
  157. const string regValueName = "SP";
  158. int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
  159. result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
  160. }
  161. return result;
  162. }
  163. private static int? GetRegValueAsInt(string regKeyName, string regValueName)
  164. {
  165. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
  166. {
  167. object value = null;
  168. if (key != null)
  169. {
  170. value = key.GetValue(regValueName);
  171. }
  172. if (value != null && value is int)
  173. {
  174. return (int)value;
  175. }
  176. else
  177. {
  178. return null;
  179. }
  180. }
  181. }
  182. private static bool CheckForRegValueEqualsInt(string regKeyName, string regValueName, int intValue)
  183. {
  184. int? value = GetRegValueAsInt(regKeyName, regValueName);
  185. if (value.HasValue)
  186. {
  187. return value.Value == intValue;
  188. }
  189. else
  190. {
  191. return false;
  192. }
  193. }
  194. public static bool CheckWindowsVersion(int major, int minor, short servicePack)
  195. {
  196. NativeStructs.OSVERSIONINFOEX osvi = new NativeStructs.OSVERSIONINFOEX();
  197. osvi.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
  198. osvi.dwMajorVersion = (uint)major;
  199. osvi.dwMinorVersion = (uint)minor;
  200. osvi.wServicePackMajor = (ushort)servicePack;
  201. ulong mask = 0;
  202. mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_MAJORVERSION, NativeConstants.VER_GREATER_EQUAL);
  203. mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_MINORVERSION, NativeConstants.VER_GREATER_EQUAL);
  204. mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_SERVICEPACKMAJOR, NativeConstants.VER_GREATER_EQUAL);
  205. bool result = NativeMethods.VerifyVersionInfo(
  206. ref osvi,
  207. NativeConstants.VER_MAJORVERSION |
  208. NativeConstants.VER_MINORVERSION |
  209. NativeConstants.VER_SERVICEPACKMAJOR,
  210. mask);
  211. return result;
  212. }
  213. // Requires:
  214. // * Windows XP SP2 or later
  215. // * Windows Server 2003 SP1 or later
  216. // * Windows Vista
  217. // * or later (must be NT-based)
  218. public static bool CheckOSRequirement()
  219. {
  220. // Just say "no" to Windows 9x
  221. if (Environment.OSVersion.Platform != PlatformID.Win32NT)
  222. {
  223. return false;
  224. }
  225. // Windows Vista or later?
  226. bool winVista = OS.CheckWindowsVersion(6, 0, 0);
  227. // Windows 2003 or later?
  228. bool win2k3 = OS.CheckWindowsVersion(5, 2, 0);
  229. // Windows 2003 SP1 or later?
  230. bool win2k3SP1 = OS.CheckWindowsVersion(5, 2, 1);
  231. // Windows XP or later?
  232. bool winXP = OS.CheckWindowsVersion(5, 1, 0);
  233. // Windows XP SP2 or later?
  234. bool winXPSP2 = OS.CheckWindowsVersion(5, 1, 2);
  235. return winVista || (win2k3 && win2k3SP1) || (winXP && winXPSP2);
  236. }
  237. }
  238. }