C#获取本地IP地址是ipv6的 怎么转换成ipv4
如题代码如下publicstringgetIP()//获取IP{stringHostName=Dns.GetHostName();//得到主机名IPHostEntryIp...
如题 代码如下 public string getIP() //获取IP
{
string HostName = Dns.GetHostName(); //得到主机名
IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //得到主机IP
string strIPAddr = IpEntry.AddressList[0].ToString();
IPAddress ipp= IPAddress.Parse(strIPAddr);
return (ipp.ToString());
}我得到的结果为fe80::49d:8fbf:ca5e:d1cd%12求各位高人怎么把它变成ipv4的地址呢?好像我调试的时候看到socket没有ipv6支持啊 展开
{
string HostName = Dns.GetHostName(); //得到主机名
IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //得到主机IP
string strIPAddr = IpEntry.AddressList[0].ToString();
IPAddress ipp= IPAddress.Parse(strIPAddr);
return (ipp.ToString());
}我得到的结果为fe80::49d:8fbf:ca5e:d1cd%12求各位高人怎么把它变成ipv4的地址呢?好像我调试的时候看到socket没有ipv6支持啊 展开
2013-04-14
展开全部
首先: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; }}
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; }}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
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);
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询