none
请问一个问题,用C#如何在不联接互联网的情况下获取到网关设备的外连口地址和MAC地址,也就是WAN口的IP地址和MAC地址 RRS feed

  • 问题

  • 找遍了所有地方,都没有找到如何获取,都是采取访问某个显示IP的网站得到WAN口的地址,但是实际情况需要,我们的运行环境下没有互联网,该怎么办?新人新手,请高手详解,CODE和调用方法,谢谢了!
    2018年8月14日 13:31

全部回复

  • To get local Ip Address:

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("No network adapters with an IPv4 address in the system!");
    }

    To check if you're connected or not:

    System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年8月15日 0:11
  • To get local Ip Address:

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("No network adapters with an IPv4 address in the system!");
    }

    To check if you're connected or not:

    System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

     need to know the external interface address of the gateway or route, but I can't connect to the Internet. Local address acquisition I know how
    2018年8月16日 2:13
  • 你好,

    你可以试试下面的方法。

    IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
    string externalIP = IPHost.AddressList[0].ToString();

    或者:

    public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }


    public string GetMACAddress()
    {
        ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMOS.Get();
        string MACAddress = String.Empty;
        foreach (ManagementObject objMO in objMOC)
        {
            if (MACAddress == String.Empty) // only return MAC Address from first card   
            {
                MACAddress = objMO["MacAddress"].ToString();
            }
            objMO.Dispose();
        }
        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年8月20日 8:10
    版主