我如何使用C#语言获取该信息
13 个解决方案
#1
帮顶
#3
好東西?
#4
感觉这种方式不是很好。最好能脱离三方支持。
#5
感觉这种方式不是很好。最好能脱离三方支持。
#6
public static IPAddress Getlocalipaddress(this IPAddress ip)
{
if(Dns.GetHostAddresses(Dns.GetHostName()).Length>1)
return Dns.GetHostAddresses(Dns.GetHostName())[1];
else
return Dns.GetHostAddresses(Dns.GetHostName())[0];
}
这是一个获得本机外网地址的扩展方法,前提是你的电脑里面只有一个网卡!
{
if(Dns.GetHostAddresses(Dns.GetHostName()).Length>1)
return Dns.GetHostAddresses(Dns.GetHostName())[1];
else
return Dns.GetHostAddresses(Dns.GetHostName())[0];
}
这是一个获得本机外网地址的扩展方法,前提是你的电脑里面只有一个网卡!
#7
你可以用调用CMD的命令来完成这个任务,调用CMD命令 ,然后写入IPCONFIG让CMD来帮你执行 ,然后你获取返回结果
#8
ccmd cm = new ccmd();
MessageBox.Show(cm.cmd("ipconfig"));
用这个命令就可以了!!
MessageBox.Show(cm.cmd("ipconfig"));
用这个命令就可以了!!
#9
网卡的方式不合适。
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i
}
catch (Exception ex)
{
}
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i
}
catch (Exception ex)
{
}
#10
using System.Net;
IPHostEntry Host = System.Net.Dns.Resolve("www.163.com");
IPEndPoint IP = new IPEndPoint(Host.AddressList[0], 0);
string s = IP.Address.ToString();//这里的S就是163的IP地址
IPHostEntry Host = System.Net.Dns.Resolve("www.163.com");
IPEndPoint IP = new IPEndPoint(Host.AddressList[0], 0);
string s = IP.Address.ToString();//这里的S就是163的IP地址
#11
static void ReportIP()
{
Console.WriteLine("****** Current IP addresses:");
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
Console.WriteLine("{0}\n SVC: '{1}' MAC: [{2}]", (string)mo["Caption"],
(string)mo["ServiceName"], (string)mo["MACAddress"]);
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
Console.WriteLine(" Addresses :");
foreach (string sad in addresses)
Console.WriteLine("\t'{0}'", sad);
// display SubNets mask
Console.WriteLine(" Subnets :");
foreach (string sub in subnets)
Console.WriteLine("\t'{0}'", sub);
}
}
System.Net.IPHostEntry MYIP = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
MessageBox("您的IP地址:" + MYIP.AddressList.GetValue(0).ToString())
MessageBox("您的计算机全名:" + MYIP.HostName.ToString())
{
Console.WriteLine("****** Current IP addresses:");
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
Console.WriteLine("{0}\n SVC: '{1}' MAC: [{2}]", (string)mo["Caption"],
(string)mo["ServiceName"], (string)mo["MACAddress"]);
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
Console.WriteLine(" Addresses :");
foreach (string sad in addresses)
Console.WriteLine("\t'{0}'", sad);
// display SubNets mask
Console.WriteLine(" Subnets :");
foreach (string sub in subnets)
Console.WriteLine("\t'{0}'", sub);
}
}
System.Net.IPHostEntry MYIP = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
MessageBox("您的IP地址:" + MYIP.AddressList.GetValue(0).ToString())
MessageBox("您的计算机全名:" + MYIP.HostName.ToString())
#12
//取一下 reply 中 []之间的内容
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.Default;
string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");
MessageBox.Show(reply);
#13
获取本机(网关)的外网IP,在C#里面实现的方法,通常是遍历获取到的本机的IP地址,然后判断,不过这种方式只能取得独立拨号或具有独立公网IP的地址,对于局域网的方式,是取不到的,这种方式的代码大致如下:
System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in ips.AddressList)
{
Console.WriteLine(ip.ToString());
}要获取本机出口的外网IP,实际是需要利用访问某个可以返回本机出口的外网IP的internet资源,例如: 本站的ip提供程序
访问此IP提供程序,将只返回一个你的出口IP的字符串,使用起来是超简单的,代码如下:
using (System.Net.WebClient wc = new System.Net.WebClient())
{
Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/"));
}
Console.ReadLine();
这个也许好点
System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in ips.AddressList)
{
Console.WriteLine(ip.ToString());
}要获取本机出口的外网IP,实际是需要利用访问某个可以返回本机出口的外网IP的internet资源,例如: 本站的ip提供程序
访问此IP提供程序,将只返回一个你的出口IP的字符串,使用起来是超简单的,代码如下:
using (System.Net.WebClient wc = new System.Net.WebClient())
{
Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/"));
}
Console.ReadLine();
这个也许好点
#1
帮顶
#2
#3
好東西?
#4
感觉这种方式不是很好。最好能脱离三方支持。
#5
感觉这种方式不是很好。最好能脱离三方支持。
#6
public static IPAddress Getlocalipaddress(this IPAddress ip)
{
if(Dns.GetHostAddresses(Dns.GetHostName()).Length>1)
return Dns.GetHostAddresses(Dns.GetHostName())[1];
else
return Dns.GetHostAddresses(Dns.GetHostName())[0];
}
这是一个获得本机外网地址的扩展方法,前提是你的电脑里面只有一个网卡!
{
if(Dns.GetHostAddresses(Dns.GetHostName()).Length>1)
return Dns.GetHostAddresses(Dns.GetHostName())[1];
else
return Dns.GetHostAddresses(Dns.GetHostName())[0];
}
这是一个获得本机外网地址的扩展方法,前提是你的电脑里面只有一个网卡!
#7
你可以用调用CMD的命令来完成这个任务,调用CMD命令 ,然后写入IPCONFIG让CMD来帮你执行 ,然后你获取返回结果
#8
ccmd cm = new ccmd();
MessageBox.Show(cm.cmd("ipconfig"));
用这个命令就可以了!!
MessageBox.Show(cm.cmd("ipconfig"));
用这个命令就可以了!!
#9
网卡的方式不合适。
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i
}
catch (Exception ex)
{
}
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i
}
catch (Exception ex)
{
}
#10
using System.Net;
IPHostEntry Host = System.Net.Dns.Resolve("www.163.com");
IPEndPoint IP = new IPEndPoint(Host.AddressList[0], 0);
string s = IP.Address.ToString();//这里的S就是163的IP地址
IPHostEntry Host = System.Net.Dns.Resolve("www.163.com");
IPEndPoint IP = new IPEndPoint(Host.AddressList[0], 0);
string s = IP.Address.ToString();//这里的S就是163的IP地址
#11
static void ReportIP()
{
Console.WriteLine("****** Current IP addresses:");
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
Console.WriteLine("{0}\n SVC: '{1}' MAC: [{2}]", (string)mo["Caption"],
(string)mo["ServiceName"], (string)mo["MACAddress"]);
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
Console.WriteLine(" Addresses :");
foreach (string sad in addresses)
Console.WriteLine("\t'{0}'", sad);
// display SubNets mask
Console.WriteLine(" Subnets :");
foreach (string sub in subnets)
Console.WriteLine("\t'{0}'", sub);
}
}
System.Net.IPHostEntry MYIP = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
MessageBox("您的IP地址:" + MYIP.AddressList.GetValue(0).ToString())
MessageBox("您的计算机全名:" + MYIP.HostName.ToString())
{
Console.WriteLine("****** Current IP addresses:");
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
Console.WriteLine("{0}\n SVC: '{1}' MAC: [{2}]", (string)mo["Caption"],
(string)mo["ServiceName"], (string)mo["MACAddress"]);
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
Console.WriteLine(" Addresses :");
foreach (string sad in addresses)
Console.WriteLine("\t'{0}'", sad);
// display SubNets mask
Console.WriteLine(" Subnets :");
foreach (string sub in subnets)
Console.WriteLine("\t'{0}'", sub);
}
}
System.Net.IPHostEntry MYIP = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
MessageBox("您的IP地址:" + MYIP.AddressList.GetValue(0).ToString())
MessageBox("您的计算机全名:" + MYIP.HostName.ToString())
#12
//取一下 reply 中 []之间的内容
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.Default;
string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");
MessageBox.Show(reply);
#13
获取本机(网关)的外网IP,在C#里面实现的方法,通常是遍历获取到的本机的IP地址,然后判断,不过这种方式只能取得独立拨号或具有独立公网IP的地址,对于局域网的方式,是取不到的,这种方式的代码大致如下:
System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in ips.AddressList)
{
Console.WriteLine(ip.ToString());
}要获取本机出口的外网IP,实际是需要利用访问某个可以返回本机出口的外网IP的internet资源,例如: 本站的ip提供程序
访问此IP提供程序,将只返回一个你的出口IP的字符串,使用起来是超简单的,代码如下:
using (System.Net.WebClient wc = new System.Net.WebClient())
{
Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/"));
}
Console.ReadLine();
这个也许好点
System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in ips.AddressList)
{
Console.WriteLine(ip.ToString());
}要获取本机出口的外网IP,实际是需要利用访问某个可以返回本机出口的外网IP的internet资源,例如: 本站的ip提供程序
访问此IP提供程序,将只返回一个你的出口IP的字符串,使用起来是超简单的,代码如下:
using (System.Net.WebClient wc = new System.Net.WebClient())
{
Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/"));
}
Console.ReadLine();
这个也许好点