wpfc#调用api获取系统版本
时间: 2023-10-28 07:18:11 浏览: 111
您可以使用以下代码调用API获取系统版本:
```csharp
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern void GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
[StructLayout(LayoutKind.Sequential)]
public 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 static string GetWindowsVersion()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
GetVersionEx(ref osVersionInfo);
string version = string.Format("{0}.{1}.{2}", osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.dwBuildNumber);
return version;
}
```
此代码段使用 `kernel32.dll` 中的 `GetVersionEx` 函数获取操作系统版本信息。如果您使用的是Windows操作系统,此代码应该可以正常工作。
阅读全文