/// <summary>
/// 通过NetworkInterface获取MAC地址
/// </summary>
/// <returns></returns>
public string GetMacByNetworkInterface()
{
try
{
string Mac = "";
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();//获取本电脑上所有的网卡信息
NetworkInterface face = interfaces.Where(x => x.Name.Contains("以太网")||x.Name.Contains("本地连接")).FirstOrDefault(); //获取本地连接
if (face == null)
{
Mac = BitConverter.ToString(interfaces[0].GetPhysicalAddress().GetAddressBytes());
}
else
{
Mac = BitConverter.ToString(face.GetPhysicalAddress().GetAddressBytes());
}
return Mac;
}
catch (Exception)
{
MessageBox.Show("如果无法确定网卡的MAC可以在定制为00-00-00-00-00-00。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return "00-00-00-00-00-00";
}
正则表达式 判读是否正确
public bool IsUsed(string str)
{
string s = @"^([0-9a-fA-F]{2})(([/\s:-][0-9a-fA-F]{2}){5})$";
Regex r = new Regex(s, RegexOptions.IgnoreCase);
Match m = r.Match(str);
return m.Success;
}
获取所有 网卡的Mac地址