c# 多网卡获取ipv4 ip地址办法列举

获取本机ip本来是很容易的,IPAddress _ip = Dns.GetHostAddresses(Dns.GetHostName())[0];就行了

但是在vista win7等系统里面这样获得的是ipv6地址,另外有多张网卡的时候问题就更复杂了

以前我都是根据自己的ip修改数组的下标,不过那样毕竟不是好的办法,也不是通用的办法,用AddressFamily来判断更好


下面我们以win7 笔记本(有无线网卡)+多网卡(vmware安装的)和win2003笔记本(有无线网卡)+多网卡(vmware安装的)

做测试

c# 多网卡获取ipv4 ip地址办法列举_第1张图片



(1)其中“测试多个”的是一种很笨的办法,获取适配器的名称,比如一般都叫“本地连接”,“无线网络连接”,遇到改名字的就无能为力了

(2)“其他办法1”的是一种简单的办法,如果单个网卡就走else,两个网卡(不区分网卡类型)走if,很傻,但是简单环境下能用,比第一种好

(3)”其他办法2“是通过数组把所有的地址读取出来放到richtextbox,应该是最全的,都能读出来

(4)“指定接口类”这个办法是根据网卡类型进行区分的,一般我们上网的网卡就是以太网卡和无线网卡,这个都能获取。网卡类型说明

参考地址:

http://msdn.microsoft.com/zh-cn/library/system.net.networkinformation.networkinterfacetype.aspx

(5)如果要具体到某一个的话建议写个扫描器吧。


using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using  System.Management;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        

        private void button3_Click(object sender, EventArgs e)
        {

            NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;
            string mip;

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                if (ni.Name == "本地连接")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();
                            label1.Text = ip.Address.ToString();
                        }
                    }
                }
                else if (ni.Name == "本地连接2")
                   {
                        IPInterfaceProperties property = ni.GetIPProperties();
                        foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                mip = ip.Address.ToString();
                                label2.Text = ip.Address.ToString();
                            }
                        }
                   }
                else if (ni.Name == "无线网络连接")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();
                            label3.Text = ip.Address.ToString();
                        }
                    }
                }
            }
            
        }

        
        private void button4_Click(object sender, EventArgs e)//简单的办法,如果单个网卡就走else,两个网卡(不区分网卡类型)走if
        {
            System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
            if (addressList.Length > 1)
            {
                label1.Text = addressList[0].ToString();
                label2.Text = addressList[1].ToString();
            }
            else
            {
                label1.Text = addressList[0].ToString();
                label2.Text = "没有可用的连接 ";
            } 
        }


       public void ShowIP()
        {
            //ipv4地址也可能不止一个
            foreach (string ip in GetLocalIpv4())
            {
                this.richTextBox1.AppendText(ip.ToString());
            }
            return;
        }
       public string[] GetLocalIpv4()
        {
            //事先不知道ip的个数,数组长度未知,因此用StringCollection储存
            try
            {
                IPAddress[] localIPs;
                localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                StringCollection IpCollection = new StringCollection();
                foreach (IPAddress ip in localIPs)
                {
                    //根据AddressFamily判断是否为ipv4,如果是InterNetWork则为ipv6
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        IpCollection.Add(ip.ToString());
                }
                string[] IpArray = new string[IpCollection.Count];
                IpCollection.CopyTo(IpArray, 0);
                return IpArray;
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            return null;
        }

       private void button5_Click(object sender, EventArgs e)
       {
           richTextBox1.Clear();//可以获取全部,列在richtextbox中
           ShowIP();
       }

       private void button7_Click(object sender, EventArgs e)
       {
           label1.Text = "";
           label2.Text = "";
           label3.Text = "";
       }

       private void button6_Click(object sender, EventArgs e)
       {
           //获取说有网卡信息
           NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
           foreach (NetworkInterface adapter in nics)
           {
               //判断是否为以太网卡
               //Wireless80211         无线网卡;    Ppp     宽带连接;Ethernet              以太网卡
               if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
               {
                   //获取以太网卡网络接口信息
                   IPInterfaceProperties ip = adapter.GetIPProperties();
                   //获取单播地址集
                   UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                   foreach (UnicastIPAddressInformation ipadd in ipCollection)
                   {
                       //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                       //Max            MAX 位址
                       if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                       label1.Text = ipadd.Address.ToString();//获取ip
                   }
               }
               else if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
               {
                   //获取以太网卡网络接口信息
                   IPInterfaceProperties ip = adapter.GetIPProperties();
                   //获取单播地址集
                   UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                   foreach (UnicastIPAddressInformation ipadd in ipCollection)
                   {
                       //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                       //Max            MAX 位址
                       if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                       label2.Text = ipadd.Address.ToString();//获取ip
                   }
               }
           }   
       }










    }
}


你可能感兴趣的:(c# 多网卡获取ipv4 ip地址办法列举)