获取 客户端的MAC地址
使用代码获取 客户端的MAC地址
方便网站获取一个用户电脑的唯一标识;统计访问的机器的情况
方便网站获取一个用户电脑的唯一标识;统计访问的机器的情况
1
#region 获取客户端MAC地址
2
/// <summary>
3
/// 获取客户端MAC地址
4
/// </summary>
5
/// <returns></returns>
6
private string GetMac()
7
{
8
string MAC = "";
9
ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
10
ManagementObjectCollection MOC = MC.GetInstances();
11
foreach (ManagementObject moc in MOC)
12
{
13
if (moc["IPEnabled"].ToString() == "True")
14
{
15
MAC = moc["MacAddress"].ToString();
16
}
17
}
18
return MAC;
19
}
20
21
22
public string GetCustomerMac(string IP) //para IP is the client's IP
23
{
24
if (IP == "127.0.0.1")
25
{
26
return GetMac();
27
}//"00-04-61-5C-31-52";//00-0F-1F-C6-B2-B3
28
else
29
{
30
string dirResults = "";
31
ProcessStartInfo psi = new ProcessStartInfo();
32
Process proc = new Process();
33
psi.FileName = "nbtstat";
34
psi.RedirectStandardInput = false;
35
psi.RedirectStandardOutput = true;
36
psi.Arguments = "-A " + IP;
37
psi.UseShellExecute = false;
38
proc = Process.Start(psi);
39
dirResults = proc.StandardOutput.ReadToEnd();
40
proc.WaitForExit();
41
dirResults = dirResults.Replace(" ", "").Replace(" ", "").Replace("\t", "");
42
int i = dirResults.LastIndexOf("=");
43
dirResults = dirResults.Substring(i + 2, 17);
44
if (dirResults.IndexOf("本地连接") != -1)
45
{ dirResults = "没有得到mac"; }
46
return dirResults;
47
}
48
}
49
50
51
52
#endregion
#region 获取客户端MAC地址2
/// <summary>3
/// 获取客户端MAC地址4
/// </summary>5
/// <returns></returns>6
private string GetMac()7
{8
string MAC = "";9
ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");10
ManagementObjectCollection MOC = MC.GetInstances();11
foreach (ManagementObject moc in MOC)12
{13
if (moc["IPEnabled"].ToString() == "True")14
{15
MAC = moc["MacAddress"].ToString();16
}17
}18
return MAC;19
}20

21

22
public string GetCustomerMac(string IP) //para IP is the client's IP23
{24
if (IP == "127.0.0.1")25
{26
return GetMac();27
}//"00-04-61-5C-31-52";//00-0F-1F-C6-B2-B3 28
else29
{30
string dirResults = "";31
ProcessStartInfo psi = new ProcessStartInfo();32
Process proc = new Process();33
psi.FileName = "nbtstat";34
psi.RedirectStandardInput = false;35
psi.RedirectStandardOutput = true;36
psi.Arguments = "-A " + IP;37
psi.UseShellExecute = false;38
proc = Process.Start(psi);39
dirResults = proc.StandardOutput.ReadToEnd();40
proc.WaitForExit();41
dirResults = dirResults.Replace(" ", "").Replace(" ", "").Replace("\t", "");42
int i = dirResults.LastIndexOf("=");43
dirResults = dirResults.Substring(i + 2, 17);44
if (dirResults.IndexOf("本地连接") != -1)45
{ dirResults = "没有得到mac"; }46
return dirResults;47
}48
}49

50

51

52
#endregion发布人信息
- 联系人:wumingwei 查看该用户发布的所有信息
- 电话:
- 邮箱:
- 地址:





