c#从配置文件读取多张网卡ip
来源:互联网 发布:湖北快三遗漏数据查询 编辑:程序博客网 时间:2024/05/01 00:14
考虑到电脑有多张网卡的情况,如果使用本机ip地址有可能就会出现获取网卡错误,所以自己写了一个从配置文件读取ip信息的代码:
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Net;using System.IO;using System.Xml;namespace ipconfig{ public class IpConfig { string m_path, m_name; public IpConfig(string path,string name)//路径名 + 文件名 c:\\config\\config.xml { m_path = path; m_name ="/"+ name; } public string GetIp(int num) { int i; string ip=""; try { if (!File.Exists(m_path + m_name)) { DirectoryInfo d = Directory.CreateDirectory(m_path); XmlDocument xmldoc = new XmlDocument(); //加入XML的声明段落 xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null)); //加入根元素 XmlElement xmlelem = xmldoc.CreateElement("", "AllIp", ""); xmldoc.AppendChild(xmlelem); //FileName IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); for (i = 1; i <= ipHost.AddressList.Length; i++) {//写ip到config。xml文件
XmlElement xmlelemFileName = xmldoc.CreateElement("Ip-" + i.ToString()); XmlText xmltextFileName = xmldoc.CreateTextNode(GetIPList(i)); xmlelemFileName.AppendChild(xmltextFileName); xmldoc.ChildNodes.Item(1).AppendChild(xmlelemFileName); } xmldoc.Save(m_path + m_name); } if (File.Exists(m_path + m_name)) ip = ReadIp(num); else ip = "can`t create ipconfig.xml"; } catch (Exception ex) { ip = ex.Message; } return ip; } public string ReadIp(int IpNum) { string strIp=""; XmlElement xe; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(m_path + m_name); XmlNode xn = xmlDoc.SelectSingleNode("AllIp"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { xe = (XmlElement)xnf; if("Ip-"+IpNum.ToString()==xnf.Name) { strIp = xe.InnerText; break; } } } catch (Exception ex) { strIp= ex.Message; } return strIp; } private string GetIPList(int netcard) //获取iplist { try { IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); //考虑ipv6的情况 主要对应win7以上系统 int netCardCount = 0; if (ipHost.AddressList.Length > 1) { string result = "255.255.255.255"; foreach (IPAddress ipaddr in ipHost.AddressList) { if (ipaddr.ToString().Length > result.Length) { netCardCount++; } } } IPAddress ipAddr = ipHost.AddressList[netcard + netCardCount - 1]; return ipAddr.ToString(); } catch (Exception ex) { return ex.Message; } } }}
- c#从配置文件读取多张网卡ip
- 关于多网卡读取IP相关信息
- 获取某张网卡的IP地址
- java获取本地IP地址,java获取本地多张网卡IP
- 从配置文件读取信息
- 从配置文件读取变量
- 刚写好的读取多网卡IP地址的函数
- 从绑定网卡获取ip
- 从绑定网卡获取ip
- C#读取XML配置文件
- C#配置文件读取
- C#读取ini配置文件
- C#读取配置文件源代码
- C#读取配置文件详解
- C#读取配置文件详解
- C# 读取ini配置文件
- c# 读取TIniFile配置文件
- C#读取配置文件详解
- oracle恢复利用重建数据文件恢复无备份的已失败的数据文件
- 1003—Max Sum
- [深入浅出Cocoa]iOS网络编程之CFNetwork
- C++构造函数虚拟化
- Maven学习(八)- 构建MyBatis项目
- c#从配置文件读取多张网卡ip
- 彻底killed掉某用户进程
- hdu 4476 Cut the rope
- 《有关搜索题目》
- 求简单链表长度!
- Java 强引用、 软引用、 弱引用、虚引用
- 函数回调方法
- 微生物增值--蓝桥杯
- PHP 持久层的简单实现