正在获取Service pack主要版本
本文关键字:版本 pack Service 获取 | 更新日期: 2023-09-27 18:24:09
在Environment.OSVersion.ServicePack中,我只看到ServicePack变量,但没有主要或次要版本,如何获得主要或次要的版本?
您可以使用以下代码。
void Main()
{
GetServicePackVersion().Major.Dump();
}
[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 short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll")]
private static extern bool GetVersionEx([In, Out] ref OSVERSIONINFOEX osVersionInfo);
public static Version GetServicePackVersion()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
if (GetVersionEx(ref osVersionInfo))
{
Version result = new Version(osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor);
return result;
}
else
{
return null;
}
}
Service pack没有主要版本或次要版本。您必须浏览Environment.OSVersion.Version
才能获得必要的版本信息。