原始程序代码
代码
/// <summary>
/// 本地的Mac地址
/// </summary>
public static string LocalMacIP
{
get
{
ManagementClass mc;
ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
str = mo["MacAddress"].ToString();
}
//return str;
return "00:19:7E:CC:ED:B5";
}
}
结果和在cmd下ipconfig的结果不同,仅是一个vm的mac
代码
string MacAddress;
//设置MAC地址
string MAC="";
ManagementClass MC = new ManagementClass ( "Win32_NetworkAdapterConfiguration" );
ManagementObjectCollection MOC = MC.GetInstances();
foreach ( ManagementObject moc in MOC )
{
if ( moc["IPEnabled"].ToString() == "True")
{
MAC = moc["MacAddress"].ToString();
}
}
MacAddress=MAC;
代码
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
string strsearch=string.Format(@"SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL)AND (Manufacturer <> 'Microsoft'))");
System.Management.ManagementObjectSearcher msearch=new ManagementObjectSearcher();
msearch.Scope=new ManagementScope(@"root\CIMV2");
msearch.Query=new ObjectQuery(strsearch);
ArrayList MacList =new ArrayList();
int i =0;
try
{
if((msearch.Get().Count<1) || (msearch.Get()==null))
{
MessageBox.Show("No MAC Address!");
}
else
{
MessageBox.Show("MAC Address No. is : "+ msearch.Get().Count.ToString());
foreach(System.Management.ManagementObject mymac in msearch.Get())
{
string str1=string.Format("MAC{0} address:{1}",i.ToString(),mymac["MACAddress"].ToString());
MessageBox.Show("MAC Address "+i.ToString()+":"+str1);
MacList.Add(str1);
i++;
}
}
}
catch(Exception ex)
{
string msg=ex.Message.ToString();
MessageBox.Show("erro", msg);
}
获得本机mac的方法,只是结果是一个ArrayList,运行的结果和cmd下命令一样,只是包括了一个真的mac和两个vm的mac
代码
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
[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);
string IPAddress=System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();
MessageBox.Show("ip: "+IPAddress);
string mac = this.GetMACFromIP(IPAddress);
MessageBox.Show("mac: "+mac);
private string GetMACFromIP(string A_strIP)
{
string strRet = "Unknown";
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";
Regex objRex =new Regex(strIPPattern);
if(objRex.IsMatch(A_strIP) == true)
{
Int32 intDest = inet_addr(A_strIP);
Int32 [] arrMAC = new Int32[2];
Int32 intLen = 6;
int intResult = SendARP(intDest,0,ref arrMAC[0],ref intLen);
if(intResult == 0)
{
Byte [] arrbyte = new Byte[8];
arrbyte[5] = (Byte)(arrMAC[1] >> 8);
arrbyte[4] = (Byte)arrMAC[1];
arrbyte[3] = (Byte)(arrMAC[0] >> 24);
arrbyte[2] = (Byte)(arrMAC[0] >> 16);
arrbyte[1] = (Byte)(arrMAC[0] >> 8);
arrbyte[0] = (Byte)arrMAC[0];
StringBuilder strbMAC = new StringBuilder();
for(int intIndex = 0 ;intIndex < 6;intIndex ++)
{
if(intIndex > 0) strbMAC.Append("-");
strbMAC.Append(arrbyte[intIndex].ToString("X2"));
}
strRet = strbMAC.ToString();
}
}
return strRet;
}
正常..