C# 区别浏览器的名称注意的
代码
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Diagnostics;
12 using System.Text.RegularExpressions;
13 using System.Net;
14 using System.IO;
15 using System.Deployment;
16 using System.Management;
17
18
19 namespace SiteMapDemo
20 {
21 public partial class WebForm10 : System.Web.UI.Page
22 {
23 protected void Page_Load(object sender, EventArgs e)
24 {
25
26
27 HttpBrowserCapabilities browser = Request.Browser;
28 //Google Chrome,Flock,Safari等浏览器无法区别是哪个浏览器名称
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Diagnostics;
12 using System.Text.RegularExpressions;
13 using System.Net;
14 using System.IO;
15 using System.Deployment;
16 using System.Management;
17
18
19 namespace SiteMapDemo
20 {
21 public partial class WebForm10 : System.Web.UI.Page
22 {
23 protected void Page_Load(object sender, EventArgs e)
24 {
25
26
27 HttpBrowserCapabilities browser = Request.Browser;
28 //Google Chrome,Flock,Safari等浏览器无法区别是哪个浏览器名称
29 Response.Write(browser.Browser.ToString() + "</br>");
30 //此方法才可以区别浏览器的名称
31 Response.Write(Request.ServerVariables["http_user_agent"].ToString());
32 //得到浏览器属性Google Chrome浏览器有五個:default,mozilla,gecko,safari,safari1plus
33 for (int i = 0; i < browser.Browsers.Count; i++)
34 {
35 Response.Write(browser.Browsers[i].ToString() + "</br>");
36 }
37 string stringMAC = "";
38 string stringIP = "";
39 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
40 ManagementObjectCollection MOC = MC.GetInstances();
41
42 foreach (ManagementObject MO in MOC)
43 {
44 if ((bool)MO["IPEnabled"] == true)
45 {
46 stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
47 string[] IPAddresses = (string[])MO["IPAddress"]; //获取本地的IP地址
48 if (IPAddresses.Length > 0)
49 stringIP = IPAddresses[0];
50 Response.Write(stringMAC + "/" + stringIP + "</br>");
51
52 }
53 }
54 string cpuInfo = "";//cpu序列号
55 ManagementClass cimobject = new ManagementClass("Win32_Processor");
56 ManagementObjectCollection moc = cimobject.GetInstances();
57 foreach (ManagementObject mo in moc)
58 {
59 cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
60 Response.Write("cpu序列号:" + cpuInfo.ToString() + "</br>");
61 }
62
63 //获取硬盘ID
64 String HDid;
65 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
66 ManagementObjectCollection moc1 = cimobject1.GetInstances();
67 foreach (ManagementObject mo in moc1)
68 {
69 HDid = (string)mo.Properties["Model"].Value;
70 Response.Write("硬盘序列号:" + HDid.ToString() + "</br>");
71 }
72
73
74 //获取网卡硬件地址
75
76 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
77 ManagementObjectCollection moc2 = mc.GetInstances();
78 foreach (ManagementObject mo in moc2)
79 {
80 if ((bool)mo["IPEnabled"] == true)
81 Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString() + "</br>");
82 mo.Dispose();
83 }
84 //在页面上打印出客户端的网卡物理地址(MAC)
85 Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
86 }
87 /// <summary>
88 /// 获取远程客户端的网卡物理地址(MAC)
89 /// </summary>
90 /// <param name="IP"></param>
91 /// <returns></returns>
92 public string GetMac(string IP) //para IP is the client's IP
93 {
94 string dirResults = "";
95 ProcessStartInfo psi = new ProcessStartInfo();
96 Process proc = new Process();
97 psi.FileName = "nbtstat";
98 psi.RedirectStandardInput = false;
99 psi.RedirectStandardOutput = true;
100 psi.Arguments = "-A " + IP;
101 psi.UseShellExecute = false;
102 proc = Process.Start(psi);
103 dirResults = proc.StandardOutput.ReadToEnd();
104 proc.WaitForExit();
105 dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
106
107 Regex reg = new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
108 Match mc = reg.Match(dirResults + "__MAC");
109
110 if (mc.Success)
111 {
112 return mc.Groups["key"].Value;
113 }
114 else
115 {
116 reg = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
117 mc = reg.Match(dirResults);
118 if (mc.Success)
119 {
120 return "Host not found!";
121 }
122 else
123 {
124 return "";
125 }
126 }
127 }
128 }
129
130
131 }
132
30 //此方法才可以区别浏览器的名称
31 Response.Write(Request.ServerVariables["http_user_agent"].ToString());
32 //得到浏览器属性Google Chrome浏览器有五個:default,mozilla,gecko,safari,safari1plus
33 for (int i = 0; i < browser.Browsers.Count; i++)
34 {
35 Response.Write(browser.Browsers[i].ToString() + "</br>");
36 }
37 string stringMAC = "";
38 string stringIP = "";
39 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
40 ManagementObjectCollection MOC = MC.GetInstances();
41
42 foreach (ManagementObject MO in MOC)
43 {
44 if ((bool)MO["IPEnabled"] == true)
45 {
46 stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
47 string[] IPAddresses = (string[])MO["IPAddress"]; //获取本地的IP地址
48 if (IPAddresses.Length > 0)
49 stringIP = IPAddresses[0];
50 Response.Write(stringMAC + "/" + stringIP + "</br>");
51
52 }
53 }
54 string cpuInfo = "";//cpu序列号
55 ManagementClass cimobject = new ManagementClass("Win32_Processor");
56 ManagementObjectCollection moc = cimobject.GetInstances();
57 foreach (ManagementObject mo in moc)
58 {
59 cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
60 Response.Write("cpu序列号:" + cpuInfo.ToString() + "</br>");
61 }
62
63 //获取硬盘ID
64 String HDid;
65 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
66 ManagementObjectCollection moc1 = cimobject1.GetInstances();
67 foreach (ManagementObject mo in moc1)
68 {
69 HDid = (string)mo.Properties["Model"].Value;
70 Response.Write("硬盘序列号:" + HDid.ToString() + "</br>");
71 }
72
73
74 //获取网卡硬件地址
75
76 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
77 ManagementObjectCollection moc2 = mc.GetInstances();
78 foreach (ManagementObject mo in moc2)
79 {
80 if ((bool)mo["IPEnabled"] == true)
81 Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString() + "</br>");
82 mo.Dispose();
83 }
84 //在页面上打印出客户端的网卡物理地址(MAC)
85 Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
86 }
87 /// <summary>
88 /// 获取远程客户端的网卡物理地址(MAC)
89 /// </summary>
90 /// <param name="IP"></param>
91 /// <returns></returns>
92 public string GetMac(string IP) //para IP is the client's IP
93 {
94 string dirResults = "";
95 ProcessStartInfo psi = new ProcessStartInfo();
96 Process proc = new Process();
97 psi.FileName = "nbtstat";
98 psi.RedirectStandardInput = false;
99 psi.RedirectStandardOutput = true;
100 psi.Arguments = "-A " + IP;
101 psi.UseShellExecute = false;
102 proc = Process.Start(psi);
103 dirResults = proc.StandardOutput.ReadToEnd();
104 proc.WaitForExit();
105 dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
106
107 Regex reg = new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
108 Match mc = reg.Match(dirResults + "__MAC");
109
110 if (mc.Success)
111 {
112 return mc.Groups["key"].Value;
113 }
114 else
115 {
116 reg = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
117 mc = reg.Match(dirResults);
118 if (mc.Success)
119 {
120 return "Host not found!";
121 }
122 else
123 {
124 return "";
125 }
126 }
127 }
128 }
129
130
131 }
132
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)