一:网上搜c# 绑定网卡Mac 有好多信息,其中有篇分为几种方法获取mac 的方法,结果获得到的是一个list 队列的信息,信息获取到所有的物理网卡,无线网卡,蓝牙,隧道的网卡物理地址。对与软件绑定物理地址来说,要的是主板网卡的物理地址一个就够了,所以上边获得了那么多没有什么鸟用。

通过命令 ipconfig /all 然后程序调用的方式,也是一大堆,更无法获取,中间有英文和中文,如果是本机操作当然没有问题,可是别的机器就玄了。

二:因为网卡绑定的方式也只能是防君子不防小人吧,mac 地址是可以改变的,本人又想了硬盘 或者主板不能改变的id信息,可惜不尽人意,有的硬盘,主板是没有序列号的,这个在网上已经有人做过实验的。所以只有mac 是几乎所有机器都会存在的。

下边的程序也是搜来的过滤掉了无线网卡用不到的那些,本人测试可行。

public static string GetMacAddressByNetworkInformation()
{
string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
string macAddress = string.Empty;
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
&& adapter.GetPhysicalAddress().ToString().Length != 0)
{
string fRegistryKey = key + adapter.Id + "\\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 &&
fPnpInstanceID.Substring(0, 3) == "PCI")
{
macAddress = adapter.GetPhysicalAddress().ToString();
for (int i = 1; i < 6; i++)
{
macAddress = macAddress.Insert(3 * i - 1, "-");
}
break;
}
}

}
}
}
catch (Exception ex)
{
//这里写异常的处理
}
return macAddress;
}

 

posted on 2016-11-03 16:02  小石头的一天  阅读(729)  评论(0编辑  收藏  举报