Get the OS version
We can get the version of the OS by using the following code
Namespace Part
using System;
using System.Runtime.InteropServices;
Code part for get the version of the OS
public class OSVersion {
public static void Main() {
MyAPI.OSVERSIONINFO MyAPIinfo = new MyAPI.OSVERSIONINFO();
MyAPIinfo.dwOSVersionInfoSize = Marshal.SizeOf(MyAPIinfo);
bool MyBool = MyAPI.GetVersionEx(ref MyAPIinfo);
if (MyBool == false) {
Console.WriteLine("GetVersion failed");
}
Console.WriteLine("{0}.{1}.{2}",
MyAPIinfo.dwMajorVersion,
MyAPIinfo.dwMinorVersion,
MyAPIinfo.dwBuildNumber);
}
}
Code for API to get the details
public class MyAPI {
[DllImport("kernel32.dll")]
public static extern
bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo);
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO {
public System.Int32 dwOSVersionInfoSize;
public System.Int32 dwMajorVersion;
public System.Int32 dwMinorVersion;
public System.Int32 dwBuildNumber;
public System.Int32 dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public String szCSDVersion;
}
}
1. Write the MyAPI class for getting the OS details
2. kernel32.dll is used to achive this target.
3. By using the we can get the Majorversion ,Miniorversion and buildnumber
By
Nathan