c#プロファイルから複数のNIC ipを読み込む

4554 ワード

パソコンに複数のNICがあることを考慮すると、ネイティブipアドレスを使用するとNIC取得エラーが発生する可能性があるので、プロファイルから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;
            }
        }
    }
}