如何在windows-CE (使用C#)中获取我的IP和服务器的IP?
发布于 2011-03-08 22:56:39
你的问题含糊不清。我不知道您想要哪种适配器或地址类型,所以我假设您想要第一个本地适配器的IPv4地址。它看起来像这样:
var ip = (from a in Dns.GetHostEntry(Dns.GetHostName()).AddressList
where a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
select a).First().ToString();要获得给定服务器的IP地址,您需要知道它的主机名,然后在该名称上进行查找(即Dns.GetHostEntry)。它看起来与上面的代码非常相似,只是您将使用服务器主机名而不是Dns.GetHostName()调用。
发布于 2011-03-08 06:10:39
它包含一些P/Invoke魔法。因为它有很多代码,而且我拥有的是部分专有的,所以我不能发布所有的代码,但我可以给出一些提示。
// MSDN: http://msdn.microsoft.com/en-us/library/aa365917(v=vs.85).aspx
[DllImport("iphlpapi.dll", SetLastError = true)]
internal static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref int pOutBufLen);
[DllImport("iphlpapi.dll", SetLastError = true)]
internal static extern int GetAdapterIndex(string AdapterName, ref int IfIndex);发布于 2016-09-07 22:01:09
我知道这个问题很老,但我最近遇到了这个问题。因为我们已经使用了OpenNETCF,所以我使用这个库来查询我的NIC及其相关地址。这是在Windows Mobile 6.5 (CE 5.2)上实现的。
var ifs = OpenNETCF.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in ifs)
{
IPAddress myip = nic.CurrentIpAddress;
}即使未配置DNS,这也会起作用。
https://stackoverflow.com/questions/5224898
复制相似问题