为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 > 其它综合 正文

c#得到本机IP的函数

三叶资源网 2022-10-31 19:18:50 其它综合 243 ℃ 0 评论

收集客户机信息时,需要得到客户机的IP,那么客户端就需要提供自己的本机IP:

using System;
using System.Net;
using System.Windows.Forms;

namespace Mynet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetMyIP());
        }
        
        /// <summary>
        /// 得到本机IP
        /// </summary>
        /// <returns></returns>
        public string GetMyIP()
        {
            try
            {
                string computername = Dns.GetHostName();
                IPHostEntry ipall = Dns.GetHostEntry(computername);
                for (int i = 0; i < ipall.AddressList.Length; i++)
                {
                    string theip = ipall.AddressList[i].ToString();
                    if (theip.LastIndexOf(".") != -1)
                    {
                        return theip;
                    }
                }
                return "未得到IP地址";
            }
            catch (Exception ErrMsg)
            {
                return ErrMsg.ToString();
            }
        }
    }
}

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
Fiddler正则表达式教程UDP源码电脑QQbilibili直播模仿打字效果CSSFX3U通信APIPOST登录超级代理服务端动画帧内涵段子GDI源码EXUI缓动登陆界面源码拖放支持库通用型软件特效易语言纯画板自动更新
最新评论