为编程爱好者分享易语言教程源码的资源网

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

c#得到本机IP的函数

三叶资源网 2022-10-31 19:18:50 其它综合 1308 ℃ 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,(三叶资源网⑤群:806035103 )

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

欢迎 你 发表评论:

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

网站分类
随机tag
支持库模版代理服务器源码动态加密算法python list界面引擎JS分析红手指云手机app算法微信群发例子HookAPP登录左右按键SQLite3cropper.jsIWebbrowser2京东M端点选源码屏幕录象递归窗口信息酷狗繁星PC协议
最新评论