随笔 - 148  文章 - 1  评论 - 15  阅读 - 30万

读取本地IP地址和子网页码

复制代码
  #region 读取本地IP地址和子网页码
            //读取本地IP地址和子网页码
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType.ToString().Equals("Ethernet"))
                {
                    //adapter.Name;     //网卡适配名称:“本地连接”
                    //adapter.Description;   //适配器描述信息
                    IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息

                    if (ip.UnicastAddresses.Count > 0)
                    {
                        string localip = ip.UnicastAddresses[1].Address.ToString();   //IP地址
                        txtIP.Text = localip;
                        string localcode = ip.UnicastAddresses[1].IPv4Mask.ToString();  //子网掩码
                        txtZWym.Text = localcode;

                    }
                    if (ip.GatewayAddresses.Count > 0)
                    {
                        string net = ip.GatewayAddresses[0].Address.ToString();   //默认网关
                        txtWg.Text = net;
                    }

                    if (ip.DnsAddresses.Count > 0)
                    {
                        ip.DnsAddresses[0].ToString();       //首选DNS服务器地址
                        if (ip.DnsAddresses.Count > 1)
                            ip.DnsAddresses[1].ToString();  //备用DNS服务器地址
                        //MessageBox.Show(" ip.DnsAddresses[0].ToString();:" + ip.DnsAddresses[0].ToString());
                    }
                }
            }
            #endregion
复制代码

 方法二:上面的方法有时候读不出来子网掩码,需要如下方法:

复制代码
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
                    string mac = nic["MacAddress"].ToString();//Mac地址

                    string ip = (nic["IPAddress"] as String[])[0];//IP地址
                    txtIP.Text = ip;
                    string ipsubnet = (nic["IPSubnet"] as String[])[0];//子网掩码
                    txtZWym.Text = ipsubnet;
                    string ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默认网关
                    txtWg.Text = ipgateway;
                }
            }
复制代码

 

posted on   冰魂雪魄  阅读(528)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF框架交流群:C#.net. WPF.core 技术交流�      C#WPF技术交流群:C#.net. WPF.core 技术交流�     WPF技术大牛交流群:C#.net. WPF.core 技术交流�
点击右上角即可分享
微信分享提示