为编程爱好者分享易语言教程源码的资源网
雨云服务器 5 折购买
雨云服务器 · 5 折购买
稳定高速 · 新老用户同享 · 点击立即抢购
立即购买
AD广告招租 了解广告位详情 ›

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

c#得到本机IP的函数

三叶资源网 2022-10-31 19:18:50 其它综合 1312 ℃ 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 )

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

欢迎 你 发表评论:

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

网站分类
随机tag
精易皮肤模块找色软件自动更新宏插件登陆查询鼠标自动点击器通信开源易语言基础教程QQ消息后台发送例子ORACLE数据库Unicode复制组件支付PHP秒杀系统进程保护python list数据库腾达路由器易语言CEF3模块
最新评论
雨云服务器 · 5 折购买
稳定高速 · 新老用户同享 · 点击立即抢购
立即购买