如何获得操作系统版本asp.net
问题描述:
我想获得浏览器打开的操作系统版本,实际上我的项目是一个asp.net项目,我想知道哪个操作系统在客户端上运行,但有一个问题关于它。因为客户端将使用XP,但同时将使用Windows CE 5.0,因此Windows CE中的Internet Explorer不如XP中的Internet Explorer,因为它会将用户重定向到我设计的页面Windows CE。那么有没有解决方案呢?如何获得操作系统版本asp.net
谢谢。
答
它的要点是使用Request.Browser.Platform
,版本在Request.UserAgent
。
答
的USER_AGENT
参数(在请求参数)应该告诉的故事。
答
使用Request.UserAgent
- 这将可能给你所需要的所有信息。
有一个"List of User-Agents"网站提供了大量的示例字符串,但是如果您的客户端具有有限的设置范围,那么值得仅仅尝试每个字符串并记录用户代理作为初步步骤。
请注意,许多浏览器将允许您“欺骗”用户代理字符串,因此您不能将其用于安全目的 - 但听起来好像您的用例非常合理。
答
OperatingSystem os = Environment.OSVersion;
var platform = os.Platform.ToString();
var version = os.Version.ToString();
var servicePack = os.ServicePack.ToString();
您还可以找到与用户代理的帮助。
String userAgent = Request.UserAgent;
if (userAgent.IndexOf("Windows NT 6.3") > 0)
{
//Windows 8.1
}
else if (userAgent.IndexOf("Windows NT 6.2") > 0)
{
//Windows 8
}
else if (userAgent.IndexOf("Windows NT 6.1") > 0)
{
//Windows 7
}
else if (userAgent.IndexOf("Windows NT 6.0") > 0)
{
//Windows Vista
}
else if (userAgent.IndexOf("Windows NT 5.2") > 0)
{
//Windows Server 2003; Windows XP x64 Edition
}
else if (userAgent.IndexOf("Windows NT 5.1") > 0)
{
//Windows XP
}
else if (userAgent.IndexOf("Windows NT 5.01") > 0)
{
//Windows 2000, Service Pack 1 (SP1)
}
else if (userAgent.IndexOf("Windows NT 5.0") > 0)
{
//Windows 2000
}
else if (userAgent.IndexOf("Windows NT 4.0") > 0)
{
//Microsoft Windows NT 4.0
}
else if (userAgent.IndexOf("Win 9x 4.90") > 0)
{
//Windows Millennium Edition (Windows Me)
}
else if (userAgent.IndexOf("Windows 98") > 0)
{
//Windows 98
}
else if (userAgent.IndexOf("Windows 95") > 0)
{
//Windows 95
}
else if (userAgent.IndexOf("Windows CE") > 0)
{
//Windows CE
}
else
{
//Others
}
答
由于所选择的答案是不是最新的,并提供一个断开的链接,我决定发布我完成它的方式:
我安装了一个名为一个很酷的工具:https://github.com/ua-parser/uap-csharp
用来解析用户代理到OS,浏览器,浏览器版本等...
这是如何使用它:
public static string GetUserBrowser(string userAgent)
{
// get a parser with the embedded regex patterns
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(userAgent);
return c.UserAgent.Family;
}
public static string GetUserOS(string userAgent)
{
// get a parser with the embedded regex patterns
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(userAgent);
return c.OS.Family;
}
阅读下面的最新解决方案我的答案。 – 2016-08-09 08:17:01