欢迎光临散文网 会员登陆 & 注册

C# 获取电脑IP地址方法

2021-08-12 08:31 作者:HK4952  | 我要投稿

public static string GetComputerIP()

        {

            string ip = "127.0.0.1";

            try

            {

                string HostName = Dns.GetHostName(); //获取主机名

                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);

                for (int i = 0; i < IpEntry.AddressList.Length; i++)

                {

                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)

                    {

                        ip = IpEntry.AddressList[i].ToString();

                        return ip;

                    }

                }

                return ip;

            }

            catch (Exception ex)

            {

                MessageBox.Show("获取本机IP异常:" + ex.Message);

                return ip;

            }

        }


C# 获取电脑IP地址方法的评论 (共 条)

分享到微博请遵守国家法律