Windows uygulamalarında uygulamamızın çalıştığı bilgisayarın MAC adresini aşağıdaki şekilde almamız mümkündür.
[System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int hedefIP, int kaynakIP, [System.Runtime.InteropServices.Out] byte[] MAC, ref int MACuzunlugu);
public string GetMAC(string bilgisayarAdi)
{
System.Net.IPAddress[] IPs = System.Net.Dns.GetHostEntry(bilgisayarAdi).AddressList;
byte[] dizi = new byte[6];
int uzunluk = dizi.Length;
SendARP((int)IPs[0].Address, 0, dizi, ref uzunluk);
return BitConverter.ToString(dizi, 0, 6);
}
Daha sonra da aşağıdaki gibi istediğimiz bi yerde mac adresini alabiliriz.
private void button4_Click(object sender, EventArgs e)
{
this.textBox2.Text = GetMAC(System.Windows.Forms.SystemInformation.ComputerName.ToString());
}
Web uygulamalarında ise aşağıdaki şekilde alabilirsini;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Runtime.InteropServices;
public class MACAddr
{
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
public static string GetMacAddress(string sName)
{
string s = string.Empty;
System.Net.IPHostEntry Tempaddr = null;Tempaddr = (System.Net.
IPHostEntry)Dns.GetHostByName(sName);
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
string[] Ipaddr = new string[3];foreach (IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP((int)TempA.Address, 0, ab, ref len);string sMAC = BitConverter.ToString(ab, 0, 6);
Ipaddr[2] = sMAC;
s = sMAC;
}
return s;
}
}
"GetMacAddress" fonksiyonunu uygulamanızda aşağıdaki şekilde çağırabilirsiniz.
lblMessage.Text = MACAddr.GetMacAddress(System.Environment.MachineName);
Hatırlatmalar:
iphlpapi.dll dosyası hakkında detaylı bilgi için http://support.microsoft.com/dllhelp/?fid=111155&l=55&det=1 adresini ziyaret edebilirsiniz. Bu adreste dll den dışarı verilen işlevler ve diğer bir takım bilgilere ulaşabilirsiniz.
1 kişi tarafından 5.0 olarak değerlendirildi
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5