/// <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;
}
给你两个方法 根据实际需要自己选 这是我之前自己封装操作类里的方法
/// <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;
}
}