using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; /* API 函数。通过IP地址获得MAC地址 */ namespace Scan { class Get_MAC { [DllImport("Ws2_32.dll")] public static extern Int32 inet_addr(string ip); [DllImport("Iphlpapi.dll")] public static extern uint SendARP([In]Int32 dest, [In]Int32 soc, ref Int64 mac, [In, Out]ref Int32 len); public Get_MAC() { } public static string GetMac(string deIP) { string mac = ""; Int64 macinfo = 0; Int32 des = inet_addr(deIP); macinfo = new Int64(); Int32 len = 6; SendARP(des, 0, ref macinfo, ref len); mac = macinfo.ToString("x4"); StringBuilder sb = new StringBuilder(); string s; int i = 0; if (mac != "0000" && mac.Length == 12) { while (i < 12) { s = mac.Substring(10 - i, 2); sb.Append(s.ToUpper()); if (i != 10) sb.Append("-"); i += 2; } mac = sb.ToString(); } else { mac = "Wrong mac!"; } return mac; } }
{
string MacAddress = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"] == true)
MacAddress = mo["MacAddress"].ToString();
mo.Dispose();
}
return MacAddress;
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
/* API 函数。通过IP地址获得MAC地址 */
namespace Scan
{
class Get_MAC
{
[DllImport("Ws2_32.dll")]
public static extern Int32 inet_addr(string ip);
[DllImport("Iphlpapi.dll")]
public static extern uint SendARP([In]Int32 dest, [In]Int32 soc, ref Int64 mac, [In, Out]ref Int32 len);
public Get_MAC()
{ }
public static string GetMac(string deIP)
{
string mac = "";
Int64 macinfo = 0;
Int32 des = inet_addr(deIP);
macinfo = new Int64();
Int32 len = 6;
SendARP(des, 0, ref macinfo, ref len);
mac = macinfo.ToString("x4");
StringBuilder sb = new StringBuilder();
string s;
int i = 0;
if (mac != "0000" && mac.Length == 12)
{
while (i < 12)
{
s = mac.Substring(10 - i, 2);
sb.Append(s.ToUpper());
if (i != 10)
sb.Append("-");
i += 2;
}
mac = sb.ToString();
}
else
{
mac = "Wrong mac!";
}
return mac;
}
}
}
--------------------------------
通过调用API函数,SendARP()。
ARP协议只能获得同一网段的MAC地址。跨网段无法获取
调用using System.Diagnostics.Process
执行命令行语句 ipconfig/all
获取返回值的某行,有MAC的...
http://topic.csdn.net/u/20081107/23/f94cdc80-1afc-4418-aa99-0da478142af0.html
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Text.RegularExpressions; //以下是三个引用
[DllImport("kernael21.dll", EntryPoint = "SetComputerNameEx")]
public static extern int SetComputerNameEx(int type, String strname); [DllImport("Iphlpapi.dll")]
private static unsafe 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); private void button1_Click(object sender, EventArgs e)
{
this.label6.Text = Dns.GetHostName();//取得机器名
this.textBox1.Text = this.label6.Text;
IPAddress[] ip = null;
ip = Dns.GetHostAddresses(this.label6.Text);//取得本机网卡IP,用这个方法获取的是机器本身网卡的IP,对此我也不知道是怎么解释
this.label8.Text = ip[0].ToString();
this.textBox3.Text = this.label8.Text; string strRet = "Unknown";
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$"; Regex objRex = new Regex(strIPPattern); if (objRex.IsMatch(this.label8.Text) == true)
{
Int32 intDest = inet_addr(this.label8.Text);
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(":");//MAC地址用“:”分隔
strbMAC.Append(arrbyte[intIndex].ToString("X2"));
}
strRet = strbMAC.ToString();//整理结果,获取MAC
this.label7.Text = strRet;
this.textBox2.Text = this.label7.Text;
}