当前位置:网站首页>Methods for obtaining some information of equipment

Methods for obtaining some information of equipment

2022-06-25 06:14:00 weixin_ forty-three million seven hundred and fifty-four thousa

Get the system Ip

public enum ADDRESSFAM
    {
    
        IPv4, IPv6
    }

    /// <summary>
    ///  Get local IP
    /// </summary>
    /// <param name="Addfam"> To obtain the IP type </param>
    /// <returns></returns>
    public string GetIP(ADDRESSFAM Addfam)
    {
    
        if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
    
            return null;
        }

        string output = "";

        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {
    
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

            if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif 
            {
    
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
    
                    //IPv4
                    if (Addfam == ADDRESSFAM.IPv4)
                    {
    
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
    
                            output = ip.Address.ToString();
                            //Debug.Log("IP:" + output);
                        }
                    }

                    //IPv6
                    else if (Addfam == ADDRESSFAM.IPv6)
                    {
    
                        if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
    
                            output = ip.Address.ToString();
                        }
                    }
                }
            }
        }
        return output;
    }

 Insert picture description here

原网站

版权声明
本文为[weixin_ forty-three million seven hundred and fifty-four thousa]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201241024649.html