C# 使用IP地址获取租赁时间
我在Windows CE 5.0和Windows Mobile 5.0上有一个应用程序,目前正在使用以下代码获取IP地址:C# 使用IP地址获取租赁时间,c#,.net,windows-ce,dhcp,windows-mobile-5.0,C#,.net,Windows Ce,Dhcp,Windows Mobile 5.0,我在Windows CE 5.0和Windows Mobile 5.0上有一个应用程序,目前正在使用以下代码获取IP地址: IPHostEntry dnsEntry = Dns.GetHostEntry(_host); foreach (IPAddress ia in dnsEntry.AddressList) { if (ia.AddressFamily == AddressFamily.InterNetwork) { _address = ia;
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
其中“_host”是从XML配置文件获取的主机名。我现在的问题是如何查看IP地址“_address”的剩余租用时间?以前从未尝试过此方法,但我认为您正在寻找
UnicastIPAddressInformation.DhcpLeaseLifetime
属性
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
请从以下方面查看此示例:
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
我保留前面的答案,因为它可能对其他人有用,但我认为在WindowsCE5.0上,使用IPHelperAPI是可能的
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
首先,看一看
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
您将使用该函数
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
它返回一个结构,该结构具有LeaseExpires
属性
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
不知道你以前是否在C#上使用过Windows API。这有点难看,但你会习惯的,只要它起作用,就好=D
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
PInvoke.NET对如何使用它有一个明确的定义。不知道它是否与桌面或CE版本相关,但我认为您可以设法使其正常工作
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
可能是这样的:
IPHostEntry dnsEntry = Dns.GetHostEntry(_host);
foreach (IPAddress ia in dnsEntry.AddressList)
{
if (ia.AddressFamily == AddressFamily.InterNetwork)
{
_address = ia;
break;
}
}
[DllImport("iphlpapi.dll", CharSet=CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);
const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
const int ERROR_BUFFER_OVERFLOW = 111;
const int MAX_ADAPTER_NAME_LENGTH = 256;
const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
const int MIB_IF_TYPE_OTHER = 1;
const int MIB_IF_TYPE_ETHERNET = 6;
const int MIB_IF_TYPE_TOKENRING = 9;
const int MIB_IF_TYPE_FDDI = 15;
const int MIB_IF_TYPE_PPP = 23;
const int MIB_IF_TYPE_LOOPBACK = 24;
const int MIB_IF_TYPE_SLIP = 28;
public static void GetAdapters()
{
long structSize = Marshal.SizeOf( typeof( IP_ADAPTER_INFO ) );
IntPtr pArray = Marshal.AllocHGlobal( new IntPtr(structSize) );
int ret = GetAdaptersInfo( pArray, ref structSize );
if (ret == ERROR_BUFFER_OVERFLOW ) // ERROR_BUFFER_OVERFLOW == 111
{
// Buffer was too small, reallocate the correct size for the buffer.
pArray = Marshal.ReAllocHGlobal( pArray, new IntPtr (structSize) );
ret = GetAdaptersInfo( pArray, ref structSize );
} // if
if ( ret == 0 )
{
// Call Succeeded
IntPtr pEntry = pArray;
do
{
// Retrieve the adapter info from the memory address
IP_ADAPTER_INFO entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure( pEntry, typeof( IP_ADAPTER_INFO ));
// ***Do something with the data HERE!***
Console.WriteLine("\n");
Console.WriteLine( "Index: {0}", entry.Index.ToString() );
// Adapter Type
string tmpString = string.Empty;
switch( entry.Type )
{
case MIB_IF_TYPE_ETHERNET : tmpString = "Ethernet"; break;
case MIB_IF_TYPE_TOKENRING : tmpString = "Token Ring"; break;
case MIB_IF_TYPE_FDDI : tmpString = "FDDI"; break;
case MIB_IF_TYPE_PPP : tmpString = "PPP"; break;
case MIB_IF_TYPE_LOOPBACK : tmpString = "Loopback"; break;
case MIB_IF_TYPE_SLIP : tmpString = "Slip"; break;
default : tmpString = "Other/Unknown"; break;
} // switch
Console.WriteLine( "Adapter Type: {0}", tmpString );
Console.WriteLine( "Name: {0}", entry.AdapterName );
Console.WriteLine( "Desc: {0}\n", entry.AdapterDescription );
Console.WriteLine( "DHCP Enabled: {0}", ( entry.DhcpEnabled == 1 ) ? "Yes" : "No" );
if (entry.DhcpEnabled == 1)
{
Console.WriteLine( "DHCP Server : {0}", entry.DhcpServer.IpAddress.Address );
// Lease Obtained (convert from "time_t" to C# DateTime)
DateTime pdatDate = new DateTime(1970, 1, 1).AddSeconds( entry.LeaseObtained ).ToLocalTime();
Console.WriteLine( "Lease Obtained: {0}", pdatDate.ToString() );
// Lease Expires (convert from "time_t" to C# DateTime)
pdatDate = new DateTime(1970, 1, 1).AddSeconds( entry.LeaseExpires ).ToLocalTime();
Console.WriteLine( "Lease Expires : {0}\n", pdatDate.ToString() );
} // if DhcpEnabled
Console.WriteLine( "IP Address : {0}", entry.IpAddressList.IpAddress.Address );
Console.WriteLine( "Subnet Mask : {0}", entry.IpAddressList.IpMask.Address );
Console.WriteLine( "Default Gateway: {0}", entry.GatewayList.IpAddress.Address );
// MAC Address (data is in a byte[])
tmpString = string.Empty;
for (int i = 0; i < entry.AddressLength - 1; i++)
{
tmpString += string.Format("{0:X2}-", entry.Address[i]);
}
Console.WriteLine( "MAC Address : {0}{1:X2}\n", tmpString, entry.Address[entry.AddressLength - 1] );
Console.WriteLine( "Has WINS: {0}", entry.HaveWins ? "Yes" : "No" );
if (entry.HaveWins)
{
Console.WriteLine( "Primary WINS Server : {0}", entry.PrimaryWinsServer.IpAddress.Address );
Console.WriteLine( "Secondary WINS Server: {0}", entry.SecondaryWinsServer.IpAddress.Address );
} // HaveWins
// Get next adapter (if any)
pEntry = entry.Next;
}
while( pEntry != IntPtr.Zero );
Marshal.FreeHGlobal(pArray);
} // if
else
{
Marshal.FreeHGlobal(pArray);
throw new InvalidOperationException( "GetAdaptersInfo failed: " + ret );
}
} // GetAdapters
[DllImport(“iphlapi.dll”,CharSet=CharSet.Ansi)]
公共静态外部int GetAdapterInfo(IntPtr pAdapterInfo,参考Int64 pBufOutLen);
const int MAX_适配器描述长度=128;
const int ERROR_BUFFER_OVERFLOW=111;
const int MAX_ADAPTER_NAME_LENGTH=256;
const int MAX_适配器地址长度=8;
const int MIB_如果类型其他=1;
如果以太网类型=6,则常数int MIB;
const int MIB_如果_TYPE_TOKENRING=9;
const int MIB_,如果类型为FDDI=15;
若类型为PPP=23,则常数int MIB;
const int MIB_如果_TYPE_环回=24;
如果类型滑差=28,则为常数int MIB;
公共静态void GetAdapters()
{
long structSize=Marshal.SizeOf(typeof(IP_适配器_信息));
IntPtr pArray=Marshal.AllocHGlobal(新的IntPtr(structSize));
int-ret=getAdapterInfo(pArray,ref-structSize);
if(ret==ERROR\u BUFFER\u OVERFLOW)//ERROR\u BUFFER\u OVERFLOW==111
{
//缓冲区太小,请重新分配缓冲区的正确大小。
pArray=Marshal.ReAllocHGlobal(pArray,新的IntPtr(structSize));
ret=GetAdapterInfo(阵列,参考结构尺寸);
}//如果
如果(ret==0)
{
//呼叫成功
IntPtr pEntry=帕雷;
做
{
//从内存地址检索适配器信息
IP_ADAPTER_INFO entry=(IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry,typeof(IP_ADAPTER_INFO));
//***对此处的数据进行处理***
Console.WriteLine(“\n”);
WriteLine(“Index:{0}”,entry.Index.ToString());
//适配器类型
字符串tmpString=string.Empty;
开关(entry.Type)
{
案例MIB_如果_类型_以太网:tmpString=“ETHERNET”中断;
案例MIB\u如果\u类型\u令牌环:tmpString=“令牌环”中断;
案例MIB如果类型为FDDI:tmpString=“FDDI”中断;
案例MIB如果类型为PPP:tmpString=“PPP”中断;
案例MIB_如果_类型_环回:tmpString=“环回”中断;
案例MIB如果类型滑动:tmpString=“SLIP”中断;
默认值:tmpString=“其他/未知”中断;
}//开关
WriteLine(“适配器类型:{0}”,tmpString);
WriteLine(“名称:{0}”,entry.AdapterName);
Console.WriteLine(“Desc:{0}\n”,entry.AdapterDescription);
WriteLine(“已启用DHCP:{0}”,(entry.DhcpEnabled==1)?“是”:“否”);
if(entry.DhcpEnabled==1)
{
WriteLine(“DHCP服务器:{0}”,entry.DhcpServer.IpAddress.Address);
//已获得租约(从“时间”转换为“日期时间”)
DateTime pdatDate=新的日期时间(1970,1,1).AddSeconds(entry.LeaseObtained).ToLocalTime();
WriteLine(“获得的租约:{0}”,pdatDate.ToString());
//租约到期(从“时间”转换为“日期时间”)
pdatDate=newdatetime(1970,1,1).AddSeconds(entry.LeaseExpires).ToLocalTime();
WriteLine(“租约到期:{0}\n”,pdatDate.ToString());
}//如果DhcpEnabled
WriteLine(“IP地址:{0}”,entry.IpAddressList.IpAddress.Address);
WriteLine(“子网掩码:{0}”,entry.IpAddressList.IpMask.Address);
WriteLine(“默认网关:{0}”,entry.GatewayList.IpAddress.Address);
//MAC地址(数据以字节[]表示)
tmpString=string.Empty;
for(int i=0;i
事实上,我已经看了很久了。我找不到正确的参考资料,结果发现一个线程声称Windows Mobile不支持System.Net.NetworkInformation。你看到这篇文章了吗?我认为这个属性在Cyramework中不支持。@包括您的链接的ANDSONPIPIMTEL似乎有方法使用C++管理CE中的IP租约。也许最好的方法是制作一个.dll,即使在看C++时,我能找到的最接近的东西是更新租约,而不是获取时间。如果可能的话,我希望避免C++。从来没用过。