C#获取本地IP地址是ipv6的 怎么转换成ipv4

2024-11-26 13:33:26
推荐回答(2个)
回答1:

首先:ipv6是在Vista,或者Win7中获取的,XP中没有ipv6;然后:不能把ipv6变成ipv4,但是你可以直接获取ipv4的值。下面是我写的一个获取ipv6和ipv4的类。你可以直接把它编译成DLL文件,以后要使用ipv4,或者ipv6的时候,就可以直接调用这个动态链接库。-------------------------------------------------------------------------------------------------------------------------------------
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.Windows.Forms;
namespace GetLocalIP{ public class GetIP { private IPAddress IPv4 , IPv6; public GetIP()//构造函数。 { getAllIP(); } private void getAllIP() { IPAddress [] ipList= Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in ipList) { //获得IPv4 if (ip.AddressFamily == AddressFamily.InterNetwork) IPv4 = ip; //获得IPv6 if (ip.AddressFamily == AddressFamily.InterNetworkV6) IPv6 = ip; } } public IPAddress GetLocalIPv4()//通过这个public函数获取ipv4 { try { if (IPv4 != null) return IPv4; else return null; } catch (Exception error) { MessageBox.Show(" GetLocalIpv4 Error: " + error.Message); return null; } } public IPAddress GetLocalIPv6()//通过这个public函数获取ipv6 { try { if (IPv6 != null) return IPv6; else return null; } catch (Exception error) { MessageBox.Show(" GetLocalIpv6 Error: " + error.Message); return null; } } }}
---------------------------用法:首先你要添加这个动态链接库.直到可以使用该类。。
GetIP getLocalIP = new GetIP();IPAddress ipv4 = getLocalIP.GetLocalIPv4();IPAddress ipv6 = getLocalIP.GetLocalIPv6();----------------------------------------------------------------------当然如果你仅仅需要获得ipv4的值的话。只需要这样就可以了。首先获取所有的Ip地址,然后从中选择ipv4.
IPAddress IPv4; IPAddress [] ipList= Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in ipList) { //获得IPv4 if (ip.AddressFamily == AddressFamily.InterNetwork) IPv4 = ip; }}

回答2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //注意ip可能有多个
            string[] localIP = new string[4];
            GetLocalIpv4(localIP);
            foreach (string ip in localIP)
            {
                if (ip != null)
                listBox1.Items.Add(ip);
            }
        }
        void GetLocalIpv4(string[] localIP)
        {
            int i = 0;
            try
            {
                IPAddress[] localIPs;
                localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                foreach (IPAddress ip in localIPs)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork) //判断是否ipv4 InterNetwork是ipv4 InterNetWorkV6是ipv6
                    localIP[i] = ip.ToString();
                    i++;
                }
            }
            catch (Exception exceptionERR)
            {
                MessageBox.Show("Error: " + exceptionERR.Message);
            }
        }
    }
}