如何获取 Windows 版本以包含在 UserAgent 字符串中

How to get the Windows version to include in a UserAgent string

我想从我的 C# 应用程序发送一个 HTTP 请求,我想要一个正确构造的用户代理字符串,以便(至少)操作系统得到很好的表示。

我发现了这个:

https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx#PltToken

这让我可以为 windows 的给定版本查找正确的字符串(我的应用程序只会在 windows 上 运行),但我肯定不会我自己查找 table(如果我的应用程序 运行 在 windows 的新版本上怎么办,我需要更新它只是为了添加一个新的 UA 字符串!

一定有一些 API 可以调用以获取当前 OS 的 UA 字符串,但我似乎找不到它。

不清楚您所说的 "well represented" 是什么意思。 Most browsers 只报告 "Windows NT $NTversion".

有关如何获取 Windows 版本,请参阅 Detect Windows version in .net and Get OS Version / Friendly Name in C#。基本上:

string versionString = "Unknown OS Version";

var osVersion = System.Environment.OSVersion;
if (osVersion.Platform == PlatformID.Win32NT)
{
    versionString = string.Format("Windows NT {0}.{1}", 
                                  osVersion.Version.Major, 
                                  osVersion.Version.Minor); 
}
else
{
    // handle non-NT Windows
}

但是,如果您的应用在 Windows 8 或更高版本(8.1、10、...)上运行,则需要 manifest your app for that version,否则它将 return 8.0 (NT 6.2).我想对于较新的 Windows 版本,您也必须为该版本重新显示。