我正在尝试从计算机检索板载以太网适配器的MAC地址,以便为设备生成唯一标识符。以下是我正在使用的方法。
NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces();
int maxHash = int.MinValue;
Guid D = Guid.Empty;
foreach (NetworkInterface net in ifConfig)
{
if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode())
{
maxHash = net.GetPhysicalAddress().ToString().GetHashCode();
ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString()));
}
}
}
但是,蓝牙适配器,VM适配器和其他几个网络适配器也属于NetworkInterfaceType.Ethernet
类型。如何专门获取板载以太网连接的MAC地址?
执行包含以省略那些是不可能的。任何帮助将不胜感激。
答案 0 :(得分:1)
我最近编写了一个 Powershell 脚本,用于为 Windows PC 生成一个永久性的系统格式不可变的唯一 ID,用于库存目的(对 ID 进行 QR 编码,将其打印在贴纸上......)。
它将 PC 制造商 UUID 字段与每个连接 PCI 的网络适配器的永久 MAC 地址连接起来(按 MAC 字母顺序排序)。 PCI 要求会自动取消可移动或虚拟 NIC。真正的脚本 MD5 对结果字符串进行哈希处理以获得更同质的标识符(恒定长度和 0-9A-F 符号集),但为了简单起见,我将在此处省略这一点
我本可以选择 PC 制造商 UUID 并收工,但我对依赖于以下内容的 ID 感到不舒服:
基本上,我试图坚持将尽可能多的具有永久和“唯一”(梦想中)标识符的不可拆卸组件放入混音器的直观想法。
此 ID 生成技术不是防故障的:如果向系统添加新的 PCI 网络适配器,ID 将更改。但随着集成 NIC 的普及,我相信这不再需要担心,如果曾经是的话。
这个解决方案并不直接适用于 C#,但我相信它可以很容易地适应,因为 Powershell 共享几乎相同的 .net 对象类生态系统。
#Variable names in spanish, but if I try to translate them in a hurry
#I will most certainly end up with a non-working script
$elpc=(Get-WmiObject -Class Win32_ComputerSystemProduct)
$id=$elpc.UUID #This is the UUID the manufacturer wrote somewhere in the BIOS
#Get network devices
#"Net" for network devices, "PCI*" because PCI connected devices begin that way
$dispositivosdered=@(Get-PnpDevice | Where-Object {($_.Class -eq "Net") -and ($_.InstanceId -like "PCI*")})
#Get network adapters
#Use "PermanentAddress" property. "MacAddress" property can be spoofed easily
$tarjetasdered=@(Get-Netadapter | Sort-Object PermanentAddress)
#Double loop to rule out any network adapters which are not PCI
#Comparison is made using long name, ie "Realtek PCIe GBE Family Controller #2"
#Is the only valid field I found to (inner) join the device and adapter worlds
for($j=0; $j -lt $tarjetasdered.length; $j++) {
for ($i=0; $i -lt $dispositivosdered.length; $i++) {
if($dispositivosdered[$i].FriendlyName -eq $tarjetasdered[$j].InterfaceDescription) {
if(-not [string]::IsNullOrEmpty($tarjetasdered[$j].PermanentAddress)) {
$id= $id + $tarjetasdered[$j].PermanentAddress.Replace("-", "");
}
}
}
}
答案 1 :(得分:0)
作为一种选择(不是最好的,但仍然=)) - 您可以尝试使用 度量。在大多数情况下,网络到物理网络的度量 卡优先
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (!(mo["Description"].ToString().Contains("VM")))
{
if (!(mo["Description"].ToString().Contains("Virtual")))
{
if (!(mo["Description"].ToString().Contains("Hyper")))
{
string[] addresses = (string[])mo["IPAddress"];
string IPConnectionMetric = Convert.ToString(mo["IPConnectionMetric"]).Trim();
foreach (string ipaddress in addresses)
{
listBox1.Items.Add(ipaddress + ";" + IPConnectionMetric);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 1)
{
int maximum = int.MinValue;
int minimum = int.MaxValue;
for (int i = 0; i < listBox1.Items.Count; i++)
{
int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]);
if ((int)output > maximum)
maximum = (int)output;
}
for (int i = 0; i < listBox1.Items.Count; i++)
{
int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]);
if ((int)output < maximum)
minimum = (int)output;
if (listBox1.Items[i].ToString().Contains(minimum.ToString()))
{
var minmetric = listBox1.Items[i].ToString();
NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces();
int maxHash = int.MinValue;
Guid D = Guid.Empty;
foreach (NetworkInterface net in ifConfig)
{
if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode())
{
maxHash = net.GetPhysicalAddress().ToString().GetHashCode();
foreach (UnicastIPAddressInformation ip in net.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
if (ip.Address.ToString().Contains(minmetric.ToString().Split(';')[0]))
{
var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString()));
}
}
else
{
NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces();
int maxHash = int.MinValue;
Guid D = Guid.Empty;
foreach (NetworkInterface net in ifConfig)
{
if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode())
{
maxHash = net.GetPhysicalAddress().ToString().GetHashCode();
var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString()));
}
}
}
答案 2 :(得分:0)
using system.Management;
private string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}