• 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);
    }
    }
    }
    }

  • 相关阅读:
    写一个Windows上的守护进程(4)日志其余
    写一个Windows上的守护进程(3)句柄的管理
    写一个Windows上的守护进程(2)单例
    写一个Windows上的守护进程(1)开篇
    Xcode 特定项目运行提示无响应
    git squash 的使用
    关于.framework 文件过大 移除包内对i386 x86_64 的支持
    git 恢复被覆盖本地提交内容
    iOS企业应用 部署配置
    关于项目中测试环境跟正式环境的区分
  • 原文地址:https://www.cnblogs.com/liubo68/p/2834129.html
Copyright © 2020-2023  润新知