using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace NX_Installation
{
class GetWindowsMainVersionInfo
{
/* API-Deklarationen */
[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public Int16 wServicePackMajor;
public Int16 wServicePackMinor;
public Int16 wSuiteMask;
public Byte wProductType;
public Byte wReserved;
}
[DllImport("kernel32")]
static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi);
private const int VER_NT_WORKSTATION = 0x0000001;
/* Aufzählung für die Windows-Hauptversion */
public enum WindowsMainVersion
{
WindowsXP,
WindowsServer2003,
WindowsXP_64,
WindowsVista,
WindowsVista_64,
WindowsServer2008,
Windows7,
Windows7_64,
WindowsServer2008R2,
Windows8,
Windows8_64,
Windows10,
Windows10_64,
WindowsServer2012,
Unknown
}
/* Ermittelt die Windows-Hauptversion */
///
/// Returns the Windows Main Version
///
///
public WindowsMainVersion GetWindowsMainVersion()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(osVersionInfo);
if (GetVersionEx(ref osVersionInfo))
{
switch (osVersionInfo.dwMajorVersion)
{
case 5:
if (Environment.OSVersion.Version.Minor == 1)
{
return WindowsMainVersion.WindowsXP;
}
else if (Environment.OSVersion.Version.Minor == 2)
{
if (osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
return WindowsMainVersion.WindowsXP_64;
}
else
{
return WindowsMainVersion.WindowsServer2003;
}
}
else
{
return WindowsMainVersion.Unknown;
}
case 6:
if (Environment.OSVersion.Version.Minor == 0)
{
if (osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if (Convert.ToString(GetPlatform()) == "X86")
{
return WindowsMainVersion.WindowsVista;
}
if (Convert.ToString(GetPlatform()) == "X64")
{
return WindowsMainVersion.WindowsVista_64;
}
else
{
throw new Exception("GetPlatform has no valid value");
}
}
else
{
return WindowsMainVersion.WindowsServer2008;
}
}
else if (Environment.OSVersion.Version.Minor == 1)
{
if (osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if (Convert.ToString(GetPlatform()) == "X86")
{
return WindowsMainVersion.Windows7;
}
if (Convert.ToString(GetPlatform()) == "X64")
{
return WindowsMainVersion.Windows7_64;
}
else
{
throw new Exception("GetPlatform has no valid value");
}
}
else
{
return WindowsMainVersion.WindowsServer2008R2;
}
}
else if (Environment.OSVersion.Version.Minor == 2)
{
if (osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if (Convert.ToString(GetPlatform()) == "X86")
{
return WindowsMainVersion.Windows8;
}
if (Convert.ToString(GetPlatform()) == "X64")
{
return WindowsMainVersion.Windows8_64;
}
else
{
throw new Exception("GetPlatform has no valid value");
}
}
else
{
return WindowsMainVersion.WindowsServer2012;
}
}
return WindowsMainVersion.Unknown;
case 10:
if (Environment.OSVersion.Version.Minor == 0)
{
//return WindowsMainVersion.Windows10_64;
if (osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if (Convert.ToString(GetPlatform()) == "X86")
{
return WindowsMainVersion.Windows10;
}
if (Convert.ToString(GetPlatform()) == "X64")
{
return WindowsMainVersion.Windows10_64;
}
else
{
throw new Exception("GetPlatform has no valid value");
}
}
else
{
return WindowsMainVersion.WindowsServer2012;
}
}
else
{
return WindowsMainVersion.Unknown;
}
default:
return WindowsMainVersion.Unknown;
}
}
else
{
throw new Exception("GetVersionEx provides false back");
}
}
public enum Platform
{
X86,
X64,
Unknown
}
internal const ushort PROCESSOR_ARCHITECTURE_INTEL = 0;
internal const ushort PROCESSOR_ARCHITECTURE_IA64 = 6;
internal const ushort PROCESSOR_ARCHITECTURE_AMD64 = 9;
internal const ushort PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF;
[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
public ushort wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public UIntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
};
[DllImport("kernel32.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);
[DllImport("kernel32.dll")]
internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
public static Platform GetPlatform()
{
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
if (System.Environment.OSVersion.Version.Major > 5 ||
(System.Environment.OSVersion.Version.Major == 5 && System.Environment.OSVersion.Version.Minor >= 1))
{
GetNativeSystemInfo(ref sysInfo);
}
else
{
GetSystemInfo(ref sysInfo);
}
switch (sysInfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_IA64:
case PROCESSOR_ARCHITECTURE_AMD64:
return Platform.X64;
case PROCESSOR_ARCHITECTURE_INTEL:
return Platform.X86;
default:
return Platform.Unknown;
}
}
}
}