OSVersionInfoClass.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. // http://www.codeproject.com/Articles/73000/Getting-Operating-System-Version-Info-Even-for-Win
  6. //https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
  7. //Thanks to Member 7861383, Scott Vickery for the Windows 8.1 update and workaround.
  8. //I have moved it to the beginning of the Name property, though...
  9. //Thakts to Brisingr Aerowing for help with the Windows 10 adapatation
  10. namespace OSVersionInfoClass
  11. {
  12. /// <summary>
  13. /// Provides detailed information about the host operating system.
  14. /// </summary>
  15. public static class OSVersionInfo
  16. {
  17. #region ENUMS
  18. public enum SoftwareArchitecture
  19. {
  20. Unknown = 0,
  21. Bit32 = 1,
  22. Bit64 = 2
  23. }
  24. public enum ProcessorArchitecture
  25. {
  26. Unknown = 0,
  27. Bit32 = 1,
  28. Bit64 = 2,
  29. Itanium64 = 3
  30. }
  31. #endregion ENUMS
  32. #region DELEGATE DECLARATION
  33. private delegate bool IsWow64ProcessDelegate([In] IntPtr handle, [Out] out bool isWow64Process);
  34. #endregion DELEGATE DECLARATION
  35. #region BITS
  36. /// <summary>
  37. /// Determines if the current application is 32 or 64-bit.
  38. /// </summary>
  39. static public SoftwareArchitecture ProgramBits
  40. {
  41. get
  42. {
  43. SoftwareArchitecture pbits = SoftwareArchitecture.Unknown;
  44. System.Collections.IDictionary test = Environment.GetEnvironmentVariables();
  45. switch (IntPtr.Size * 8)
  46. {
  47. case 64:
  48. pbits = SoftwareArchitecture.Bit64;
  49. break;
  50. case 32:
  51. pbits = SoftwareArchitecture.Bit32;
  52. break;
  53. default:
  54. pbits = SoftwareArchitecture.Unknown;
  55. break;
  56. }
  57. return pbits;
  58. }
  59. }
  60. static public SoftwareArchitecture OSBits
  61. {
  62. get
  63. {
  64. SoftwareArchitecture osbits = SoftwareArchitecture.Unknown;
  65. switch (IntPtr.Size * 8)
  66. {
  67. case 64:
  68. osbits = SoftwareArchitecture.Bit64;
  69. break;
  70. case 32:
  71. if (Is32BitProcessOn64BitProcessor())
  72. osbits = SoftwareArchitecture.Bit64;
  73. else
  74. osbits = SoftwareArchitecture.Bit32;
  75. break;
  76. default:
  77. osbits = SoftwareArchitecture.Unknown;
  78. break;
  79. }
  80. return osbits;
  81. }
  82. }
  83. /// <summary>
  84. /// Determines if the current processor is 32 or 64-bit.
  85. /// </summary>
  86. static public ProcessorArchitecture ProcessorBits
  87. {
  88. get
  89. {
  90. ProcessorArchitecture pbits = ProcessorArchitecture.Unknown;
  91. try
  92. {
  93. SYSTEM_INFO l_System_Info = new SYSTEM_INFO();
  94. GetNativeSystemInfo(ref l_System_Info);
  95. switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
  96. {
  97. case 9: // PROCESSOR_ARCHITECTURE_AMD64
  98. pbits = ProcessorArchitecture.Bit64;
  99. break;
  100. case 6: // PROCESSOR_ARCHITECTURE_IA64
  101. pbits = ProcessorArchitecture.Itanium64;
  102. break;
  103. case 0: // PROCESSOR_ARCHITECTURE_INTEL
  104. pbits = ProcessorArchitecture.Bit32;
  105. break;
  106. default: // PROCESSOR_ARCHITECTURE_UNKNOWN
  107. pbits = ProcessorArchitecture.Unknown;
  108. break;
  109. }
  110. }
  111. catch
  112. {
  113. // Ignore
  114. }
  115. return pbits;
  116. }
  117. }
  118. #endregion BITS
  119. #region EDITION
  120. static private string s_Edition;
  121. /// <summary>
  122. /// Gets the edition of the operating system running on this computer.
  123. /// </summary>
  124. static public string Edition
  125. {
  126. get
  127. {
  128. if (s_Edition != null)
  129. return s_Edition; //***** RETURN *****//
  130. string edition = String.Empty;
  131. OperatingSystem osVersion = Environment.OSVersion;
  132. OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
  133. osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
  134. if (GetVersionEx(ref osVersionInfo))
  135. {
  136. int majorVersion = osVersion.Version.Major;
  137. int minorVersion = osVersion.Version.Minor;
  138. byte productType = osVersionInfo.wProductType;
  139. short suiteMask = osVersionInfo.wSuiteMask;
  140. #region VERSION 4
  141. if (majorVersion == 4)
  142. {
  143. if (productType == VER_NT_WORKSTATION)
  144. {
  145. // Windows NT 4.0 Workstation
  146. edition = "Workstation";
  147. }
  148. else if (productType == VER_NT_SERVER)
  149. {
  150. if ((suiteMask & VER_SUITE_ENTERPRISE) != 0)
  151. {
  152. // Windows NT 4.0 Server Enterprise
  153. edition = "Enterprise Server";
  154. }
  155. else
  156. {
  157. // Windows NT 4.0 Server
  158. edition = "Standard Server";
  159. }
  160. }
  161. }
  162. #endregion VERSION 4
  163. #region VERSION 5
  164. else if (majorVersion == 5)
  165. {
  166. if (productType == VER_NT_WORKSTATION)
  167. {
  168. if ((suiteMask & VER_SUITE_PERSONAL) != 0)
  169. {
  170. edition = "Home";
  171. }
  172. else
  173. {
  174. if (GetSystemMetrics(86) == 0) // 86 == SM_TABLETPC
  175. edition = "Professional";
  176. else
  177. edition = "Tablet Edition";
  178. }
  179. }
  180. else if (productType == VER_NT_SERVER)
  181. {
  182. if (minorVersion == 0)
  183. {
  184. if ((suiteMask & VER_SUITE_DATACENTER) != 0)
  185. {
  186. // Windows 2000 Datacenter Server
  187. edition = "Datacenter Server";
  188. }
  189. else if ((suiteMask & VER_SUITE_ENTERPRISE) != 0)
  190. {
  191. // Windows 2000 Advanced Server
  192. edition = "Advanced Server";
  193. }
  194. else
  195. {
  196. // Windows 2000 Server
  197. edition = "Server";
  198. }
  199. }
  200. else
  201. {
  202. if ((suiteMask & VER_SUITE_DATACENTER) != 0)
  203. {
  204. // Windows Server 2003 Datacenter Edition
  205. edition = "Datacenter";
  206. }
  207. else if ((suiteMask & VER_SUITE_ENTERPRISE) != 0)
  208. {
  209. // Windows Server 2003 Enterprise Edition
  210. edition = "Enterprise";
  211. }
  212. else if ((suiteMask & VER_SUITE_BLADE) != 0)
  213. {
  214. // Windows Server 2003 Web Edition
  215. edition = "Web Edition";
  216. }
  217. else
  218. {
  219. // Windows Server 2003 Standard Edition
  220. edition = "Standard";
  221. }
  222. }
  223. }
  224. }
  225. #endregion VERSION 5
  226. #region VERSION 6
  227. else if (majorVersion == 6)
  228. {
  229. int ed;
  230. if (GetProductInfo(majorVersion, minorVersion,
  231. osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor,
  232. out ed))
  233. {
  234. switch (ed)
  235. {
  236. case PRODUCT_BUSINESS:
  237. edition = "Business";
  238. break;
  239. case PRODUCT_BUSINESS_N:
  240. edition = "Business N";
  241. break;
  242. case PRODUCT_CLUSTER_SERVER:
  243. edition = "HPC Edition";
  244. break;
  245. case PRODUCT_CLUSTER_SERVER_V:
  246. edition = "HPC Edition without Hyper-V";
  247. break;
  248. case PRODUCT_DATACENTER_SERVER:
  249. edition = "Datacenter Server";
  250. break;
  251. case PRODUCT_DATACENTER_SERVER_CORE:
  252. edition = "Datacenter Server (core installation)";
  253. break;
  254. case PRODUCT_DATACENTER_SERVER_V:
  255. edition = "Datacenter Server without Hyper-V";
  256. break;
  257. case PRODUCT_DATACENTER_SERVER_CORE_V:
  258. edition = "Datacenter Server without Hyper-V (core installation)";
  259. break;
  260. case PRODUCT_EMBEDDED:
  261. edition = "Embedded";
  262. break;
  263. case PRODUCT_ENTERPRISE:
  264. edition = "Enterprise";
  265. break;
  266. case PRODUCT_ENTERPRISE_N:
  267. edition = "Enterprise N";
  268. break;
  269. case PRODUCT_ENTERPRISE_E:
  270. edition = "Enterprise E";
  271. break;
  272. case PRODUCT_ENTERPRISE_SERVER:
  273. edition = "Enterprise Server";
  274. break;
  275. case PRODUCT_ENTERPRISE_SERVER_CORE:
  276. edition = "Enterprise Server (core installation)";
  277. break;
  278. case PRODUCT_ENTERPRISE_SERVER_CORE_V:
  279. edition = "Enterprise Server without Hyper-V (core installation)";
  280. break;
  281. case PRODUCT_ENTERPRISE_SERVER_IA64:
  282. edition = "Enterprise Server for Itanium-based Systems";
  283. break;
  284. case PRODUCT_ENTERPRISE_SERVER_V:
  285. edition = "Enterprise Server without Hyper-V";
  286. break;
  287. case PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT:
  288. edition = "Essential Business Server MGMT";
  289. break;
  290. case PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL:
  291. edition = "Essential Business Server ADDL";
  292. break;
  293. case PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC:
  294. edition = "Essential Business Server MGMTSVC";
  295. break;
  296. case PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC:
  297. edition = "Essential Business Server ADDLSVC";
  298. break;
  299. case PRODUCT_HOME_BASIC:
  300. edition = "Home Basic";
  301. break;
  302. case PRODUCT_HOME_BASIC_N:
  303. edition = "Home Basic N";
  304. break;
  305. case PRODUCT_HOME_BASIC_E:
  306. edition = "Home Basic E";
  307. break;
  308. case PRODUCT_HOME_PREMIUM:
  309. edition = "Home Premium";
  310. break;
  311. case PRODUCT_HOME_PREMIUM_N:
  312. edition = "Home Premium N";
  313. break;
  314. case PRODUCT_HOME_PREMIUM_E:
  315. edition = "Home Premium E";
  316. break;
  317. case PRODUCT_HOME_PREMIUM_SERVER:
  318. edition = "Home Premium Server";
  319. break;
  320. case PRODUCT_HYPERV:
  321. edition = "Microsoft Hyper-V Server";
  322. break;
  323. case PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT:
  324. edition = "Windows Essential Business Management Server";
  325. break;
  326. case PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING:
  327. edition = "Windows Essential Business Messaging Server";
  328. break;
  329. case PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY:
  330. edition = "Windows Essential Business Security Server";
  331. break;
  332. case PRODUCT_PROFESSIONAL:
  333. edition = "Professional";
  334. break;
  335. case PRODUCT_PROFESSIONAL_N:
  336. edition = "Professional N";
  337. break;
  338. case PRODUCT_PROFESSIONAL_E:
  339. edition = "Professional E";
  340. break;
  341. case PRODUCT_SB_SOLUTION_SERVER:
  342. edition = "SB Solution Server";
  343. break;
  344. case PRODUCT_SB_SOLUTION_SERVER_EM:
  345. edition = "SB Solution Server EM";
  346. break;
  347. case PRODUCT_SERVER_FOR_SB_SOLUTIONS:
  348. edition = "Server for SB Solutions";
  349. break;
  350. case PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM:
  351. edition = "Server for SB Solutions EM";
  352. break;
  353. case PRODUCT_SERVER_FOR_SMALLBUSINESS:
  354. edition = "Windows Essential Server Solutions";
  355. break;
  356. case PRODUCT_SERVER_FOR_SMALLBUSINESS_V:
  357. edition = "Windows Essential Server Solutions without Hyper-V";
  358. break;
  359. case PRODUCT_SERVER_FOUNDATION:
  360. edition = "Server Foundation";
  361. break;
  362. case PRODUCT_SMALLBUSINESS_SERVER:
  363. edition = "Windows Small Business Server";
  364. break;
  365. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  366. edition = "Windows Small Business Server Premium";
  367. break;
  368. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE:
  369. edition = "Windows Small Business Server Premium (core installation)";
  370. break;
  371. case PRODUCT_SOLUTION_EMBEDDEDSERVER:
  372. edition = "Solution Embedded Server";
  373. break;
  374. case PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE:
  375. edition = "Solution Embedded Server (core installation)";
  376. break;
  377. case PRODUCT_STANDARD_SERVER:
  378. edition = "Standard Server";
  379. break;
  380. case PRODUCT_STANDARD_SERVER_CORE:
  381. edition = "Standard Server (core installation)";
  382. break;
  383. case PRODUCT_STANDARD_SERVER_SOLUTIONS:
  384. edition = "Standard Server Solutions";
  385. break;
  386. case PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE:
  387. edition = "Standard Server Solutions (core installation)";
  388. break;
  389. case PRODUCT_STANDARD_SERVER_CORE_V:
  390. edition = "Standard Server without Hyper-V (core installation)";
  391. break;
  392. case PRODUCT_STANDARD_SERVER_V:
  393. edition = "Standard Server without Hyper-V";
  394. break;
  395. case PRODUCT_STARTER:
  396. edition = "Starter";
  397. break;
  398. case PRODUCT_STARTER_N:
  399. edition = "Starter N";
  400. break;
  401. case PRODUCT_STARTER_E:
  402. edition = "Starter E";
  403. break;
  404. case PRODUCT_STORAGE_ENTERPRISE_SERVER:
  405. edition = "Enterprise Storage Server";
  406. break;
  407. case PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE:
  408. edition = "Enterprise Storage Server (core installation)";
  409. break;
  410. case PRODUCT_STORAGE_EXPRESS_SERVER:
  411. edition = "Express Storage Server";
  412. break;
  413. case PRODUCT_STORAGE_EXPRESS_SERVER_CORE:
  414. edition = "Express Storage Server (core installation)";
  415. break;
  416. case PRODUCT_STORAGE_STANDARD_SERVER:
  417. edition = "Standard Storage Server";
  418. break;
  419. case PRODUCT_STORAGE_STANDARD_SERVER_CORE:
  420. edition = "Standard Storage Server (core installation)";
  421. break;
  422. case PRODUCT_STORAGE_WORKGROUP_SERVER:
  423. edition = "Workgroup Storage Server";
  424. break;
  425. case PRODUCT_STORAGE_WORKGROUP_SERVER_CORE:
  426. edition = "Workgroup Storage Server (core installation)";
  427. break;
  428. case PRODUCT_UNDEFINED:
  429. edition = "Unknown product";
  430. break;
  431. case PRODUCT_ULTIMATE:
  432. edition = "Ultimate";
  433. break;
  434. case PRODUCT_ULTIMATE_N:
  435. edition = "Ultimate N";
  436. break;
  437. case PRODUCT_ULTIMATE_E:
  438. edition = "Ultimate E";
  439. break;
  440. case PRODUCT_WEB_SERVER:
  441. edition = "Web Server";
  442. break;
  443. case PRODUCT_WEB_SERVER_CORE:
  444. edition = "Web Server (core installation)";
  445. break;
  446. }
  447. }
  448. }
  449. #endregion VERSION 6
  450. }
  451. s_Edition = edition;
  452. return edition;
  453. }
  454. }
  455. #endregion EDITION
  456. #region NAME
  457. static private string s_Name;
  458. /// <summary>
  459. /// Gets the name of the operating system running on this computer.
  460. /// </summary>
  461. static public string Name
  462. {
  463. get
  464. {
  465. if (s_Name != null)
  466. return s_Name; //***** RETURN *****//
  467. string name = "unknown";
  468. OperatingSystem osVersion = Environment.OSVersion;
  469. OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
  470. osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
  471. if (GetVersionEx(ref osVersionInfo))
  472. {
  473. int majorVersion = osVersion.Version.Major;
  474. int minorVersion = osVersion.Version.Minor;
  475. if (majorVersion == 6 && minorVersion == 2)
  476. {
  477. //The registry read workaround is by Scott Vickery. Thanks a lot for the help!
  478. //http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
  479. // For applications that have been manifested for Windows 8.1 & Windows 10. Applications not manifested for 8.1 or 10 will return the Windows 8 OS version value (6.2).
  480. // By reading the registry, we'll get the exact version - meaning we can even compare against Win 8 and Win 8.1.
  481. string exactVersion = RegistryRead(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", "");
  482. if (!string.IsNullOrEmpty(exactVersion))
  483. {
  484. string[] splitResult = exactVersion.Split('.');
  485. majorVersion = Convert.ToInt32(splitResult[0]);
  486. minorVersion = Convert.ToInt32(splitResult[1]);
  487. }
  488. if (IsWindows10())
  489. {
  490. majorVersion = 10;
  491. minorVersion = 0;
  492. }
  493. }
  494. switch (osVersion.Platform)
  495. {
  496. case PlatformID.Win32S:
  497. name = "Windows 3.1";
  498. break;
  499. case PlatformID.WinCE:
  500. name = "Windows CE";
  501. break;
  502. case PlatformID.Win32Windows:
  503. {
  504. if (majorVersion == 4)
  505. {
  506. string csdVersion = osVersionInfo.szCSDVersion;
  507. switch (minorVersion)
  508. {
  509. case 0:
  510. if (csdVersion == "B" || csdVersion == "C")
  511. name = "Windows 95 OSR2";
  512. else
  513. name = "Windows 95";
  514. break;
  515. case 10:
  516. if (csdVersion == "A")
  517. name = "Windows 98 Second Edition";
  518. else
  519. name = "Windows 98";
  520. break;
  521. case 90:
  522. name = "Windows Me";
  523. break;
  524. }
  525. }
  526. break;
  527. }
  528. case PlatformID.Win32NT:
  529. {
  530. byte productType = osVersionInfo.wProductType;
  531. switch (majorVersion)
  532. {
  533. case 3:
  534. name = "Windows NT 3.51";
  535. break;
  536. case 4:
  537. switch (productType)
  538. {
  539. case 1:
  540. name = "Windows NT 4.0";
  541. break;
  542. case 3:
  543. name = "Windows NT 4.0 Server";
  544. break;
  545. }
  546. break;
  547. case 5:
  548. switch (minorVersion)
  549. {
  550. case 0:
  551. name = "Windows 2000";
  552. break;
  553. case 1:
  554. name = "Windows XP";
  555. break;
  556. case 2:
  557. name = "Windows Server 2003";
  558. break;
  559. }
  560. break;
  561. case 6:
  562. switch (minorVersion)
  563. {
  564. case 0:
  565. switch (productType)
  566. {
  567. case 1:
  568. name = "Windows Vista";
  569. break;
  570. case 3:
  571. name = "Windows Server 2008";
  572. break;
  573. }
  574. break;
  575. case 1:
  576. switch (productType)
  577. {
  578. case 1:
  579. name = "Windows 7";
  580. break;
  581. case 3:
  582. name = "Windows Server 2008 R2";
  583. break;
  584. }
  585. break;
  586. case 2:
  587. switch (productType)
  588. {
  589. case 1:
  590. name = "Windows 8";
  591. break;
  592. case 3:
  593. name = "Windows Server 2012";
  594. break;
  595. }
  596. break;
  597. case 3:
  598. switch (productType)
  599. {
  600. case 1:
  601. name = "Windows 8.1";
  602. break;
  603. case 3:
  604. name = "Windows Server 2012 R2";
  605. break;
  606. }
  607. break;
  608. }
  609. break;
  610. case 10:
  611. switch (minorVersion)
  612. {
  613. case 0:
  614. switch (productType)
  615. {
  616. case 1:
  617. name = "Windows 10";
  618. break;
  619. case 3:
  620. name = "Windows Server 2016";
  621. break;
  622. }
  623. break;
  624. }
  625. break;
  626. }
  627. break;
  628. }
  629. }
  630. }
  631. s_Name = name;
  632. return name;
  633. }
  634. }
  635. #endregion NAME
  636. #region PINVOKE
  637. #region GET
  638. #region PRODUCT INFO
  639. [DllImport("Kernel32.dll")]
  640. internal static extern bool GetProductInfo(
  641. int osMajorVersion,
  642. int osMinorVersion,
  643. int spMajorVersion,
  644. int spMinorVersion,
  645. out int edition);
  646. #endregion PRODUCT INFO
  647. #region VERSION
  648. [DllImport("kernel32.dll")]
  649. private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
  650. #endregion VERSION
  651. #region SYSTEMMETRICS
  652. [DllImport("user32")]
  653. public static extern int GetSystemMetrics(int nIndex);
  654. #endregion SYSTEMMETRICS
  655. #region SYSTEMINFO
  656. [DllImport("kernel32.dll")]
  657. public static extern void GetSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);
  658. [DllImport("kernel32.dll")]
  659. public static extern void GetNativeSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);
  660. #endregion SYSTEMINFO
  661. #endregion GET
  662. #region OSVERSIONINFOEX
  663. [StructLayout(LayoutKind.Sequential)]
  664. private struct OSVERSIONINFOEX
  665. {
  666. public int dwOSVersionInfoSize;
  667. public int dwMajorVersion;
  668. public int dwMinorVersion;
  669. public int dwBuildNumber;
  670. public int dwPlatformId;
  671. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  672. public string szCSDVersion;
  673. public short wServicePackMajor;
  674. public short wServicePackMinor;
  675. public short wSuiteMask;
  676. public byte wProductType;
  677. public byte wReserved;
  678. }
  679. #endregion OSVERSIONINFOEX
  680. #region SYSTEM_INFO
  681. [StructLayout(LayoutKind.Sequential)]
  682. public struct SYSTEM_INFO
  683. {
  684. internal _PROCESSOR_INFO_UNION uProcessorInfo;
  685. public uint dwPageSize;
  686. public IntPtr lpMinimumApplicationAddress;
  687. public IntPtr lpMaximumApplicationAddress;
  688. public IntPtr dwActiveProcessorMask;
  689. public uint dwNumberOfProcessors;
  690. public uint dwProcessorType;
  691. public uint dwAllocationGranularity;
  692. public ushort dwProcessorLevel;
  693. public ushort dwProcessorRevision;
  694. }
  695. #endregion SYSTEM_INFO
  696. #region _PROCESSOR_INFO_UNION
  697. [StructLayout(LayoutKind.Explicit)]
  698. public struct _PROCESSOR_INFO_UNION
  699. {
  700. [FieldOffset(0)]
  701. internal uint dwOemId;
  702. [FieldOffset(0)]
  703. internal ushort wProcessorArchitecture;
  704. [FieldOffset(2)]
  705. internal ushort wReserved;
  706. }
  707. #endregion _PROCESSOR_INFO_UNION
  708. #region 64 BIT OS DETECTION
  709. [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  710. public extern static IntPtr LoadLibrary(string libraryName);
  711. [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  712. public extern static IntPtr GetProcAddress(IntPtr hwnd, string procedureName);
  713. #endregion 64 BIT OS DETECTION
  714. #region PRODUCT
  715. private const int PRODUCT_UNDEFINED = 0x00000000;
  716. private const int PRODUCT_ULTIMATE = 0x00000001;
  717. private const int PRODUCT_HOME_BASIC = 0x00000002;
  718. private const int PRODUCT_HOME_PREMIUM = 0x00000003;
  719. private const int PRODUCT_ENTERPRISE = 0x00000004;
  720. private const int PRODUCT_HOME_BASIC_N = 0x00000005;
  721. private const int PRODUCT_BUSINESS = 0x00000006;
  722. private const int PRODUCT_STANDARD_SERVER = 0x00000007;
  723. private const int PRODUCT_DATACENTER_SERVER = 0x00000008;
  724. private const int PRODUCT_SMALLBUSINESS_SERVER = 0x00000009;
  725. private const int PRODUCT_ENTERPRISE_SERVER = 0x0000000A;
  726. private const int PRODUCT_STARTER = 0x0000000B;
  727. private const int PRODUCT_DATACENTER_SERVER_CORE = 0x0000000C;
  728. private const int PRODUCT_STANDARD_SERVER_CORE = 0x0000000D;
  729. private const int PRODUCT_ENTERPRISE_SERVER_CORE = 0x0000000E;
  730. private const int PRODUCT_ENTERPRISE_SERVER_IA64 = 0x0000000F;
  731. private const int PRODUCT_BUSINESS_N = 0x00000010;
  732. private const int PRODUCT_WEB_SERVER = 0x00000011;
  733. private const int PRODUCT_CLUSTER_SERVER = 0x00000012;
  734. private const int PRODUCT_HOME_SERVER = 0x00000013;
  735. private const int PRODUCT_STORAGE_EXPRESS_SERVER = 0x00000014;
  736. private const int PRODUCT_STORAGE_STANDARD_SERVER = 0x00000015;
  737. private const int PRODUCT_STORAGE_WORKGROUP_SERVER = 0x00000016;
  738. private const int PRODUCT_STORAGE_ENTERPRISE_SERVER = 0x00000017;
  739. private const int PRODUCT_SERVER_FOR_SMALLBUSINESS = 0x00000018;
  740. private const int PRODUCT_SMALLBUSINESS_SERVER_PREMIUM = 0x00000019;
  741. private const int PRODUCT_HOME_PREMIUM_N = 0x0000001A;
  742. private const int PRODUCT_ENTERPRISE_N = 0x0000001B;
  743. private const int PRODUCT_ULTIMATE_N = 0x0000001C;
  744. private const int PRODUCT_WEB_SERVER_CORE = 0x0000001D;
  745. private const int PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT = 0x0000001E;
  746. private const int PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY = 0x0000001F;
  747. private const int PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING = 0x00000020;
  748. private const int PRODUCT_SERVER_FOUNDATION = 0x00000021;
  749. private const int PRODUCT_HOME_PREMIUM_SERVER = 0x00000022;
  750. private const int PRODUCT_SERVER_FOR_SMALLBUSINESS_V = 0x00000023;
  751. private const int PRODUCT_STANDARD_SERVER_V = 0x00000024;
  752. private const int PRODUCT_DATACENTER_SERVER_V = 0x00000025;
  753. private const int PRODUCT_ENTERPRISE_SERVER_V = 0x00000026;
  754. private const int PRODUCT_DATACENTER_SERVER_CORE_V = 0x00000027;
  755. private const int PRODUCT_STANDARD_SERVER_CORE_V = 0x00000028;
  756. private const int PRODUCT_ENTERPRISE_SERVER_CORE_V = 0x00000029;
  757. private const int PRODUCT_HYPERV = 0x0000002A;
  758. private const int PRODUCT_STORAGE_EXPRESS_SERVER_CORE = 0x0000002B;
  759. private const int PRODUCT_STORAGE_STANDARD_SERVER_CORE = 0x0000002C;
  760. private const int PRODUCT_STORAGE_WORKGROUP_SERVER_CORE = 0x0000002D;
  761. private const int PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE = 0x0000002E;
  762. private const int PRODUCT_STARTER_N = 0x0000002F;
  763. private const int PRODUCT_PROFESSIONAL = 0x00000030;
  764. private const int PRODUCT_PROFESSIONAL_N = 0x00000031;
  765. private const int PRODUCT_SB_SOLUTION_SERVER = 0x00000032;
  766. private const int PRODUCT_SERVER_FOR_SB_SOLUTIONS = 0x00000033;
  767. private const int PRODUCT_STANDARD_SERVER_SOLUTIONS = 0x00000034;
  768. private const int PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE = 0x00000035;
  769. private const int PRODUCT_SB_SOLUTION_SERVER_EM = 0x00000036;
  770. private const int PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM = 0x00000037;
  771. private const int PRODUCT_SOLUTION_EMBEDDEDSERVER = 0x00000038;
  772. private const int PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE = 0x00000039;
  773. //private const int ???? = 0x0000003A;
  774. private const int PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT = 0x0000003B;
  775. private const int PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL = 0x0000003C;
  776. private const int PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC = 0x0000003D;
  777. private const int PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC = 0x0000003E;
  778. private const int PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE = 0x0000003F;
  779. private const int PRODUCT_CLUSTER_SERVER_V = 0x00000040;
  780. private const int PRODUCT_EMBEDDED = 0x00000041;
  781. private const int PRODUCT_STARTER_E = 0x00000042;
  782. private const int PRODUCT_HOME_BASIC_E = 0x00000043;
  783. private const int PRODUCT_HOME_PREMIUM_E = 0x00000044;
  784. private const int PRODUCT_PROFESSIONAL_E = 0x00000045;
  785. private const int PRODUCT_ENTERPRISE_E = 0x00000046;
  786. private const int PRODUCT_ULTIMATE_E = 0x00000047;
  787. //private const int PRODUCT_UNLICENSED = 0xABCDABCD;
  788. #endregion PRODUCT
  789. #region VERSIONS
  790. private const int VER_NT_WORKSTATION = 1;
  791. private const int VER_NT_DOMAIN_CONTROLLER = 2;
  792. private const int VER_NT_SERVER = 3;
  793. private const int VER_SUITE_SMALLBUSINESS = 1;
  794. private const int VER_SUITE_ENTERPRISE = 2;
  795. private const int VER_SUITE_TERMINAL = 16;
  796. private const int VER_SUITE_DATACENTER = 128;
  797. private const int VER_SUITE_SINGLEUSERTS = 256;
  798. private const int VER_SUITE_PERSONAL = 512;
  799. private const int VER_SUITE_BLADE = 1024;
  800. #endregion VERSIONS
  801. #endregion PINVOKE
  802. #region SERVICE PACK
  803. /// <summary>
  804. /// Gets the service pack information of the operating system running on this computer.
  805. /// </summary>
  806. static public string ServicePack
  807. {
  808. get
  809. {
  810. string servicePack = String.Empty;
  811. OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
  812. osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
  813. if (GetVersionEx(ref osVersionInfo))
  814. {
  815. servicePack = osVersionInfo.szCSDVersion;
  816. }
  817. return servicePack;
  818. }
  819. }
  820. #endregion SERVICE PACK
  821. #region VERSION
  822. #region BUILD
  823. /// <summary>
  824. /// Gets the build version number of the operating system running on this computer.
  825. /// </summary>
  826. static public int BuildVersion
  827. {
  828. get
  829. {
  830. return int.Parse(RegistryRead(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuildNumber", "0"));
  831. }
  832. }
  833. #endregion BUILD
  834. #region FULL
  835. #region STRING
  836. /// <summary>
  837. /// Gets the full version string of the operating system running on this computer.
  838. /// </summary>
  839. static public string VersionString
  840. {
  841. get
  842. {
  843. return Version.ToString();
  844. }
  845. }
  846. #endregion STRING
  847. #region VERSION
  848. /// <summary>
  849. /// Gets the full version of the operating system running on this computer.
  850. /// </summary>
  851. static public Version Version
  852. {
  853. get
  854. {
  855. return new Version(MajorVersion, MinorVersion, BuildVersion, RevisionVersion);
  856. }
  857. }
  858. #endregion VERSION
  859. #endregion FULL
  860. #region MAJOR
  861. /// <summary>
  862. /// Gets the major version number of the operating system running on this computer.
  863. /// </summary>
  864. static public int MajorVersion
  865. {
  866. get
  867. {
  868. if(IsWindows10())
  869. {
  870. return 10;
  871. }
  872. string exactVersion = RegistryRead(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", "");
  873. if(!string.IsNullOrEmpty(exactVersion))
  874. {
  875. string[] splitVersion = exactVersion.Split('.');
  876. return int.Parse(splitVersion[0]);
  877. }
  878. return Environment.OSVersion.Version.Major;
  879. }
  880. }
  881. #endregion MAJOR
  882. #region MINOR
  883. /// <summary>
  884. /// Gets the minor version number of the operating system running on this computer.
  885. /// </summary>
  886. static public int MinorVersion
  887. {
  888. get
  889. {
  890. if (IsWindows10())
  891. {
  892. return 0;
  893. }
  894. string exactVersion = RegistryRead(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", "");
  895. if (!string.IsNullOrEmpty(exactVersion))
  896. {
  897. string[] splitVersion = exactVersion.Split('.');
  898. return int.Parse(splitVersion[1]);
  899. }
  900. return Environment.OSVersion.Version.Minor;
  901. }
  902. }
  903. #endregion MINOR
  904. #region REVISION
  905. /// <summary>
  906. /// Gets the revision version number of the operating system running on this computer.
  907. /// </summary>
  908. static public int RevisionVersion
  909. {
  910. get
  911. {
  912. if(IsWindows10())
  913. {
  914. return 0;
  915. }
  916. return Environment.OSVersion.Version.Revision;
  917. }
  918. }
  919. #endregion REVISION
  920. #endregion VERSION
  921. #region 64 BIT OS DETECTION
  922. private static IsWow64ProcessDelegate GetIsWow64ProcessDelegate()
  923. {
  924. IntPtr handle = LoadLibrary("kernel32");
  925. if (handle != IntPtr.Zero)
  926. {
  927. IntPtr fnPtr = GetProcAddress(handle, "IsWow64Process");
  928. if (fnPtr != IntPtr.Zero)
  929. {
  930. return (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)fnPtr, typeof(IsWow64ProcessDelegate));
  931. }
  932. }
  933. return null;
  934. }
  935. private static bool Is32BitProcessOn64BitProcessor()
  936. {
  937. IsWow64ProcessDelegate fnDelegate = GetIsWow64ProcessDelegate();
  938. if (fnDelegate == null)
  939. {
  940. return false;
  941. }
  942. bool isWow64;
  943. bool retVal = fnDelegate.Invoke(Process.GetCurrentProcess().Handle, out isWow64);
  944. if (retVal == false)
  945. {
  946. return false;
  947. }
  948. return isWow64;
  949. }
  950. #endregion 64 BIT OS DETECTION
  951. #region Windows 10 Detection
  952. private static bool IsWindows10()
  953. {
  954. string productName = RegistryRead(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "");
  955. if (productName.StartsWith("Windows 10", StringComparison.OrdinalIgnoreCase))
  956. {
  957. return true;
  958. }
  959. return false;
  960. }
  961. #endregion
  962. #region Registry Methods
  963. private static string RegistryRead(string RegistryPath, string Field, string DefaultValue)
  964. {
  965. string rtn = "";
  966. string backSlash = "";
  967. string newRegistryPath = "";
  968. try
  969. {
  970. RegistryKey OurKey = null;
  971. string[] split_result = RegistryPath.Split('\\');
  972. if (split_result.Length > 0)
  973. {
  974. split_result[0] = split_result[0].ToUpper(); // Make the first entry uppercase...
  975. if (split_result[0] == "HKEY_CLASSES_ROOT") OurKey = Registry.ClassesRoot;
  976. else if (split_result[0] == "HKEY_CURRENT_USER") OurKey = Registry.CurrentUser;
  977. else if (split_result[0] == "HKEY_LOCAL_MACHINE") OurKey = Registry.LocalMachine;
  978. else if (split_result[0] == "HKEY_USERS") OurKey = Registry.Users;
  979. else if (split_result[0] == "HKEY_CURRENT_CONFIG") OurKey = Registry.CurrentConfig;
  980. if (OurKey != null)
  981. {
  982. for (int i = 1; i < split_result.Length; i++)
  983. {
  984. newRegistryPath += backSlash + split_result[i];
  985. backSlash = "\\";
  986. }
  987. if (newRegistryPath != "")
  988. {
  989. //rtn = (string)Registry.GetValue(RegistryPath, "CurrentVersion", DefaultValue);
  990. OurKey = OurKey.OpenSubKey(newRegistryPath);
  991. rtn = (string)OurKey.GetValue(Field, DefaultValue);
  992. OurKey.Close();
  993. }
  994. }
  995. }
  996. }
  997. catch { }
  998. return rtn;
  999. }
  1000. #endregion Registry Methods
  1001. }
  1002. }