C#取MAC

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
getIP gi = new getIP();
Console.WriteLine("本地網卡信息:");
Console.WriteLine(gi.getLocalIP() + " - " + gi.getLocalMac());

Console.WriteLine("\n\r遠程網卡信息:");
string[] temp = gi.getRemoteIP("scmobile-tj2");
for (int i = 0; i < temp.Length; i++)
{
Console.WriteLine(temp[i]);
}
Console.WriteLine(gi.getRemoteMac(gi.getLocalIP(), "192.168.3.101"));
}

}
public class getIP
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);

//獲取本機的IP
public string getLocalIP()
{
string strHostName = Dns.GetHostName(); //得到本機的主機名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機IP
string strAddr = ipEntry.AddressList[0].ToString();
return (strAddr);
}
//獲取本機的MAC
public string getLocalMac()
{
string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
}

//獲取遠程主機IP
public string[] getRemoteIP(string RemoteHostName)
{
IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
IPAddress[] IpAddr = ipEntry.AddressList;
string[] strAddr = new string[IpAddr.Length];
for (int i = 0; i < IpAddr.Length; i++)
{
strAddr[i] = IpAddr[i].ToString();
}
return (strAddr);
}
//獲取遠程主機MAC
public string getRemoteMac(string localIP, string remoteIP)
{
Int32 ldest = inet_addr(remoteIP); //目的ip
Int32 lhost = inet_addr(localIP); //本地ip

try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
return Convert.ToString(macinfo, 16);
}
catch (Exception err)
{
Console.WriteLine("Error:{0}", err.Message);
}
return 0.ToString();
}
}

}





C# 獲取 MAC地址!
作者:10CN.NET 來源:博客園 發佈時間:2005-10-09 11:21 閱讀:202 次 原文鏈接 [收藏]

最近項目中需要一個方法,來獲取本機的MAC地址。
本人最後找到兩種方法來實現此功能:

   方法一:直接獲取
using System;
using System.Management;

namespace PublicBill.GetMAC
{
class GetMAC
{
[STAThread]
static void Main(string[] args)
{
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if(mo["IPEnabled"].ToString() == "True")
{
mac = mo["MacAddress"].ToString();
}
}
Console.WriteLine("MAC Address: " + mac.ToString());
}
}
}




   方法二:ARP協議的解析原理獲取MAC地址
using System;
using System.Runtime.InteropServices;
using System.Net;

namespace GetMacAddress
{
class MAC
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref ulong mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);

[STAThread]
static void Main(string[] args)
{
IPHostEntry hostInfo = Dns.GetHostByName(Dns.GetHostName());
IPAddress[] addrs = hostInfo.AddressList;
IPAddress ipAddress = addrs[0];

Int32 ldest=inet_addr(ipAddress.ToString());

try
{
Int32 length = 6;
ulong mac = 0;
IntPtr len = new IntPtr(length);
int ii=SendARP(ldest,0,ref mac,ref len);
string l = "";
string h = "";
int n = 1;
long m = 0;
long lm = (long)mac;
while(lm>0)
{
m = lm%16;
lm = lm/16;
if(m>9)
{
if(m==10)l="A"+l;
if(m==11)l="B"+l;
if(m==12)l="C"+l;
if(m==13)l="D"+l;
if(m==14)l="E"+l;
if(m==15)l="F"+l;
}
else
{
l = m.ToString() + 1;
}
if((n%2)==0)
{
h = h + 1;
l = "";
}
++n;
}
Console.WriteLine(" IP Address: " + ipAddress.ToString());
Console.WriteLine("MAC Address: " + h);
}
catch(Exception error)
{
Console.WriteLine(error);
}
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章