using System;
using System.Management;
namespace getip
{
class Program
{
static void Main(string[] args)
{
try
{
//获取IP地址
string st = null;
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);
st = ar.GetValue(0).ToString();
break;
}
}
moc = null;
mc = null;
if (!String.IsNullOrWhiteSpace("st"))
{
Console.WriteLine("本机ip:" + st);
}
else
{
Console.WriteLine("本机ip:");
}
}
catch (Exception ex)
{
Console.WriteLine("获取本机ip异常:" + ex.Message);
}
finally
{ }
}
}
}