Detecting operating system version using C#
Posted @ Mar 22, 2008 10:56 AM | Permalink
Out of the blue your boss wants you to code something in .NET that will detect the version of the operating system. He need this for some module his working on. So you did your research and found out that there is this class in .NET called Environment.OSVersion that returns the Operating System information of a certain machine. Upon examining what this particular class has to offer you found out that it only gives you the version number and the platform type. Pretty useless, huh? Well not really. The values of these 2 properties are actually what we need in order to get the OS information. The only thing missing is the mapping of the versions and platform. Below is the code for detecting the OS version. It is written in C# though.

1
2 public class Enums
3 {
4 public enum WinType
5 {
6 x86,
7 x64
8 }
9
10 // Add more version if you want
11 public enum WinVersion
12 {
13 Win95,
14 Win98,
15 WinME,
16 WinNT4,
17 Win2000,
18 WinXP,
19 Vista
20 }
21 }


Here is the mapping of versions and platform:

1
2 case PlatformID.Win32NT:
3 switch (os.Version.Major) {
4 case 4:
5 // Windows NT
6 return new WinInfo(
7 Enums.WinVersion.WinNT4,
8 winType
9 );
10 case 5:
11 if (os.Version.Minor == 0) {
12 // Windows 2000
13 return new WinInfo(
14 Enums.WinVersion.Win2000,
15 winType
16 );
17 }
18 else {
19 // Windows XP
20 return new WinInfo(
21 Enums.WinVersion.WinXP,
22 winType
23 );
24 }
25 default: // 6
26 // Vista
27 return new WinInfo(
28 Enums.WinVersion.Vista,
29 winType
30 );
31 }
32 // And so on...


Well, that's about it. You can download the complete code here - source

Windows version reference - Microsoft Windows

No Comments

Leave a Comment

(optional)

4 + 2 =
Enter the sum of the 2 numbers above.