• 获取本机mac和Ip地址


    using System;

    using System.Collections.Generic;

    using System.Linq; using System.Text;

    using System.Runtime.InteropServices;

    using System.Net;

    namespace getMac

    {

         public static class Class1

        {

            //获取mac地址

            public static string getMyMac()

            {

                string ip = GetIP(); //本机ip地址

                int dest = inet_addr(ip);

                int num2 = inet_addr("");

                long mac = 0L;

                int length = 6;

                int num5 = SendARP(dest, 0, ref mac, ref length); //1.目标IP地址 2.源IP地址 3.返回MAC地址指针 4.返回MAC地址长度

                string str2 = mac.ToString("X"); //转换成十六进制

                if (str2 == "0")

                {

                    return "Localhost";

                }

                while (str2.Length < 12)

                {

                    str2 = str2.Insert(0, "0");

                }

                string str3 = "";

                for (int i = 0; i < 11; i++)

                {

                    if ((i % 2) == 0)

                    {

                        if (i == 0)

                        {

                            str3 = str3.Insert(0, str2.Substring(i, 2));

                        }

                        else

                        {

                            str3 = str3.Insert(0, str2.Substring(i, 2) + ":");

                        }

                    }

                }

                return str3;

            }

            private static string GetIP()   //获取本地IP

            {

                IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());

                IPAddress ipAddr = ipHost.AddressList[0];

                return ipAddr.ToString();

            }

            [DllImport("Ws2_32.dll")]

            private static extern int inet_addr(string ip);

            [DllImport("Iphlpapi.dll")]

            private static extern int SendARP(int dest, int host, ref long mac, ref int length);

        }

    }

    //别忘了在bin目录下放入Ws2_32.dll和Iphlpapi.dll,一般这两个控件在本地电脑的C:\WINDOWS\system32目录下,如果没有你可以在本地电脑搜搜,在没有就到网上下载

  • 相关阅读:
    爬虫遇到取到网页为reload的问题
    【自用】爬虫配置XML时拼接URL中文转Unicode问题(例如北京转成%u5317%u4EAC)
    Could not find artifact com.sun:tools:jar:1.5.0解决方法
    在XP系统下搭建maven环境出的问题 Unable to locate the Javac Compiler in: C:Program FilesJavajre6..lib ools.jar
    如何修改latex标题,作者以及正文的间距
    LaTeX排版札记
    校验码
    Computer Architecture
    Latex 显示字体和背景设置
    Gurobi + CVX + Matlab
  • 原文地址:https://www.cnblogs.com/hpp3501/p/2720192.html
Copyright © 2020-2023  润新知