62,074
社区成员
发帖
与我相关
我的任务
分享
//获取本机真实MAC地址代码如下:
using System;
using System.Runtime.InteropServices;
public partial class _Default : System.Web.UI.Page
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int32 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
protected void Page_Load(object sender, EventArgs e)
{
string IPAddress = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();
string mac = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = "-a " + IPAddress;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
int len = output.IndexOf("MAC 地址 = ");
if (len > 0)
{
mac = output.Substring(len + 8, 18);
}
this.txtmac.Text = mac;
}
}