C# 如何获取操作系统版本asp.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/561528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get operating system version asp.net
提问by mehmetserif
I want to get the os version that the browser opens, actually my project is an asp.net project and i want to know which operating system runs on the client but there is a question about it. Because the client will use xp but at the same time will use Windows CE 5.0, so the internet explorer in Windows CE is not as good as the one in xp, because of it i'll redirect the user to the page that i designed for Windows CE. So is there any solution to do it?
我想获取浏览器打开的os版本,实际上我的项目是一个asp.net项目,我想知道客户端上运行的是哪个操作系统但有一个问题。因为客户端会使用xp但同时会使用Windows CE 5.0,所以Windows CE中的Internet Explorer不如xp中的好,因为它我会将用户重定向到我设计的页面视窗 CE。那么有什么解决方案可以做到吗?
Thank you..
谢谢..
采纳答案by Tim Sullivan
The gist of it is use Request.Browser.Platform
, and the version is in Request.UserAgent
.
它的要点是 use Request.Browser.Platform
,版本在Request.UserAgent
.
回答by Assaf Lavie
The USER_AGENT
parameter (on of the request parameters) should tell the story.
该USER_AGENT
参数(在请求参数)应该告诉的故事。
回答by Jon Skeet
Use Request.UserAgent
- that will probablygive all the information you need.
使用Request.UserAgent
- 这可能会提供您需要的所有信息。
There's a "List of User-Agents"web site which gives lots of sample strings, but if your client has a limited range of setups, it would be worth just trying each of them and logging the user agent as a preliminary step.
有一个“用户代理列表”网站,它提供了许多示例字符串,但是如果您的客户端的设置范围有限,那么值得尝试每个设置并记录用户代理作为初步步骤。
Be aware that many browsers will allow you to "spoof" the user agent string, so you mustn't use this for security purposes - but it sounds as if your use case is pretty reasonable.
请注意,许多浏览器允许您“欺骗”用户代理字符串,因此您不能出于安全目的使用它 - 但听起来好像您的用例非常合理。
回答by Prashant Atal
i used this and it works just fine. I have used it in one of my apps.
我用过这个,效果很好。我在我的一个应用程序中使用过它。
回答by Musakkhir Sayyed
OperatingSystem os = Environment.OSVersion;
var platform = os.Platform.ToString();
var version = os.Version.ToString();
var servicePack = os.ServicePack.ToString();
You can also find with the help of user agent.
您还可以在用户代理的帮助下找到。
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
}
回答by Offir Pe'er
Since the selected answer is not up to date and supplied a broken link I have decided to publish the way I accomplished it:
由于选择的答案不是最新的并且提供了一个断开的链接,我决定发布我完成它的方式:
I installed a cool tool named: https://github.com/ua-parser/uap-csharp
that parse the user agent to OS,Browser,Browser version etc...
我安装了一个很酷的工具,名为:https: //github.com/ua-parser/uap-csharp
,它将用户代理解析为操作系统、浏览器、浏览器版本等...
And this is how used it:
这是如何使用它:
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;
}