Posted By

sharathreddyp on 10/05/10


Tagged

os Information


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Tyster
ovuncgursoy


Get OS Info


 / Published in: C#
 

  1. string getOSInfo()
  2. {
  3. //Get Operating system information.
  4. OperatingSystem os = Environment.OSVersion;
  5. //Get version information about the os.
  6. Version vs = os.Version;
  7.  
  8. //Variable to hold our return value
  9. string operatingSystem = "";
  10.  
  11. if (os.Platform == PlatformID.Win32Windows)
  12. {
  13. //This is a pre-NT version of Windows
  14. switch (vs.Minor)
  15. {
  16. case 0:
  17. operatingSystem = "95";
  18. break;
  19. case 10:
  20. if (vs.Revision.ToString() == "2222A")
  21. operatingSystem = "98SE";
  22. else
  23. operatingSystem = "98";
  24. break;
  25. case 90:
  26. operatingSystem = "Me";
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. else if (os.Platform == PlatformID.Win32NT)
  33. {
  34. switch (vs.Major)
  35. {
  36. case 3:
  37. operatingSystem = "NT 3.51";
  38. break;
  39. case 4:
  40. operatingSystem = "NT 4.0";
  41. break;
  42. case 5:
  43. if (vs.Minor == 0)
  44. operatingSystem = "2000";
  45. else
  46. operatingSystem = "XP";
  47. break;
  48. case 6:
  49. if (vs.Minor == 0)
  50. operatingSystem = "Vista";
  51. else
  52. operatingSystem = "7";
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. //Make sure we actually got something in our OS check
  59. //We don't want to just return " Service Pack 2" or " 32-bit"
  60. //That information is useless without the OS version.
  61. if (operatingSystem != "")
  62. {
  63. //Got something. Let's prepend "Windows" and get more info.
  64. operatingSystem = "Windows " + operatingSystem;
  65. //See if there's a service pack installed.
  66. if (os.ServicePack != "")
  67. {
  68. //Append it to the OS name. i.e. "Windows XP Service Pack 3"
  69. operatingSystem += " " + os.ServicePack;
  70. }
  71. //Append the OS architecture. i.e. "Windows XP Service Pack 3 32-bit"
  72. operatingSystem += " " + getOSArchitecture().ToString() + "-bit";
  73. }
  74. //Return the information we've gathered.
  75. return operatingSystem;
  76. }
  77. int getOSArchitecture()
  78. {
  79. string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
  80. return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? 32 : 64);
  81. }

Report this snippet  

You need to login to post a comment.