首页 新闻 搜索 专区 学院

C#获取ip地址。

0
[已解决问题] 解决于 2018-04-10 11:44

IPHostEntry me = Dns.GetHostEntry(clientinfo.computername);
foreach (IPAddress ip in me.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
clientinfo.ip = ip.ToString();
}

以上获取所有的ipv4地址,怎么修改能从ipv4地址中筛选出虚拟地址和真实地址,只要真实的ipv4地址

yjcd的主页 yjcd | 初学一级 | 园豆:61
提问于:2017-08-08 14:29
< >
分享
最佳答案
0

/// <summary>
/// 获取本机ip列表
/// </summary>
/// <returns></returns>
public static string GetHostIP()
{
try
{
string ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily== System.Net.Sockets.AddressFamily.InterNetwork).ToString();//IP地址
return ip;
}
catch (Exception ex)
{
throw new Exception("获取本机IP异常", ex);
}
}
/// <summary>
/// 获取本机ip列表
/// </summary>
/// <returns></returns>
public static List<string> GetHostIPList()
{
try
{
string hostname = Dns.GetHostName();
List<string> ipList = new List<string>();
System.Net.IPHostEntry ipEntry = Dns.GetHostEntry(hostname);

foreach (IPAddress ip in ipEntry.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(ip.ToString());
}
}

return ipList;
}
catch (Exception ex)
{
throw new Exception("获取本机IP异常", ex);
}
}
#endregion

[System.Runtime.InteropServices.DllImport("Iphlpapi.dll")]
static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
/// <summary>
/// SendArp获取MAC地址
/// </summary>
/// <returns></returns>
public static string GetMacAddressBySendARP()
{
StringBuilder strReturn = new StringBuilder();
try
{
System.Net.IPHostEntry Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(Dns.GetHostName());
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
Int32 remote = (int)TempAd[0].Address;
Int64 macinfo = new Int64();
Int32 length = 6;
SendARP(remote, 0, ref macinfo, ref length);

string temp = System.Convert.ToString(macinfo, 16).PadLeft(12, '0').ToUpper();

int x = 12;
for (int i = 0; i < 6; i++)
{
if (i == 5) { strReturn.Append(temp.Substring(x - 2, 2)); }
else { strReturn.Append(temp.Substring(x - 2, 2) + ":"); }
x -= 2;
}
return strReturn.ToString();
}
catch
{
return "";
}
}
/// <summary>
/// 获取本地计算机的名称
/// </summary>
/// <returns></returns>
public static string GetHostName()
{
return Dns.GetHostName();
}
/// <summary>
/// 获取mac地址第二种方法
/// </summary>
/// <returns></returns>
public static List<string> GetMacByNetworkInterface()
{
List<string> macs =new List<string>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{ macs.Add(ni.GetPhysicalAddress().ToString());
}
return macs;
}

奖励园豆:5
凌空万物 | 菜鸟二级 |园豆:209 | 2017-08-22 16:25
其他回答(1)
0

给你两个方法 根据实际需要自己选 这是我之前自己封装操作类里的方法

 

/// <summary>
/// 获取当前启用的IP地址
/// 可通过OperationHelper.Windows.HardwareInfo.E 获取是否出现异常
/// </summary>
/// <returns></returns>
public static List<string> GetIPAddress()
{
List<string> ls = new List<string>();
try
{
_e = null;
//获取IP地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
{
//st=mo["IpAddress"].ToString();
System.Array ar;
ar=(System.Array)(mo.Properties["IpAddress"].Value);
string st = ar.GetValue(0).ToString();
ls.Add(st);
break;
}
}
moc=null;
mc=null;
return ls ;
}
catch(Exception e)
{
_e = e;
ls.Add("unknow");
return ls;
}
finally
{
}

}

/// <summary>
/// 获取所有IP列表
/// 可通过OperationHelper.Windows.HardwareInfo.E 获取是否出现异常
/// </summary>
/// <param name="IsIPV6Address">是否获取IPV6的IP 否表示获取IPV4</param>
/// <returns></returns>
public static List<IPAddress> GetIPList(Boolean IsIPV6Address)
{
try
{
_e = null;
string hostName = Dns.GetHostName(); //获取本机名
IPHostEntry localhost;
if (IsIPV6Address)
{
localhost = Dns.GetHostEntry(hostName); //获取IPv6地址
}
else
{
localhost = Dns.GetHostByName(hostName); //方法已过期,可以获取IPv4的地址
}
return localhost.AddressList.ToList();
}
catch (Exception e) {
_e = e;
return null;
}
}

我丫的是条鱼 | 园豆:240 (菜鸟二级) | 2017-08-08 14:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册