C# – Get Windows Version and Edition

When we want to find out information about our windows operating system, we can simply enter the “winver” command in command prompt but how we can get these details with c#. So I would like to introduce a code snippet which comes from http://www.csharp411.com/determine-windows-version-and-edition-with-c/ to help us enumerate all information about current windows operating system. I modified this class a little so that it can recognize Windows 7 too because the version of that post does not realize Windows 7.
The main concept of this OSInfo class is using two important API functions

internal static extern bool GetProductInfo(int osMajorVersion,int osMinorVersion,int spMajorVersion,int spMinorVersion,out int edition);
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

GetProductInfo Function retrieves the product type for the operating system on the local computer, and maps the type to the product types supported by the specified operating system. To retrieve product type information on versions of Windows prior to the minimum supported operating systems specified in the Requirements section, use the GetVersionEx function. You can also use the OperatingSystemSKU property of the Win32_OperatingSystem WMI class.
GetVersionEx Function retrieves information about the current operating system. Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.
To demonstrate how to use this class I wrote a small code snippet below which shows all provided properties

string strOSInfo =
"Operating system: " + OSInfo.Bits.ToString() + " bits" + Environment.NewLine +
"Build version: " + OSInfo.BuildVersion.ToString() + Environment.NewLine +
"Edition: " + OSInfo.Edition.ToString() + Environment.NewLine +
"Major Version: " + OSInfo.MajorVersion.ToString() + Environment.NewLine +
"Minor Version: " + OSInfo.MinorVersion.ToString() + Environment.NewLine +
"Name: " + OSInfo.Name + Environment.NewLine +
"Revision Version: " + OSInfo.RevisionVersion.ToString() + Environment.NewLine +
"Service pack: " + OSInfo.ServicePack + Environment.NewLine +
"Version: " + OSInfo.VersionString;
txtbOSInfo.Text = strOSInfo;

The complete source code you can download here “Windows Version Edition”.

1 comment

Leave a comment

Your email address will not be published. Required fields are marked *