C#获取本机IP的6种方法
print?1、//GetHostName
private void GetIP()
{
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
listBox1.Items.Add(ip.ToString());
}
}
private void GetIP()
{
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
listBox1.Items.Add(ip.ToString());
}
}
1、//GetHostName
private void GetIP()
{
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
listBox1.Items.Add(ip.ToString());
}
}
2、使用IPHostEntry获取本机局域网地址
private void GetIP()
{
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
listBox1.Items.Add(ip.ToString());
}
}
2、使用IPHostEntry获取本机局域网地址
[csharp]
static string GetLocalIp()
拼魔方的技巧 {
string hostname = Dns.GetHostName();//得到本机名
//IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已过期,只得到IPv4的地址
IPHostEntry localhost = Dns.GetHostEntry(hostname);
IPAddress localaddr = localhost.AddressList[0];
return localaddr.ToString();
}
拼魔方的技巧 {
string hostname = Dns.GetHostName();//得到本机名
//IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已过期,只得到IPv4的地址
IPHostEntry localhost = Dns.GetHostEntry(hostname);
IPAddress localaddr = localhost.AddressList[0];
return localaddr.ToString();
}
static string GetLocalIp()
{
string hostname = Dns.GetHostName();//得到本机名
//IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已过期,只得到IPv4的地址
IPHostEntry localhost = Dns.GetHostEntry(hostname);
{
string hostname = Dns.GetHostName();//得到本机名
//IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已过期,只得到IPv4的地址
IPHostEntry localhost = Dns.GetHostEntry(hostname);
IPAddress localaddr = localhost.AddressList[0];
return localaddr.ToString();
}
3、获取本机网络ip地址
return localaddr.ToString();
}
3、获取本机网络ip地址
[csharp]
string strUrl = "提供IP查询的网站的链接";
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq .GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
int i = all.IndexOf("[") + 1;
//分析字符串得到IP
return ip;
/*
string strUrl = "提供IP查询的网站的链接";
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq .GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
int i = all.IndexOf("[") + 1;
//分析字符串得到IP
return ip;
/*
我用的是www.ip/getip.php?action=getip&ip_url=&from=web
(这种链接很容易的,百度“IP”得到一些网站,分析一下网站的链接就能得到)
返回的数据是:
<div class="well"><p>当前 IP:<code>0.0.0.0</code> 来自:XX省XX市 电信</p><p>GeoIP: Beijing, China</p></div>
解析这段就行
*/
(这种链接很容易的,百度“IP”得到一些网站,分析一下网站的链接就能得到)
返回的数据是:
<div class="well"><p>当前 IP:<code>0.0.0.0</code> 来自:XX省XX市 电信</p><p>GeoIP: Beijing, China</p></div>
解析这段就行
*/
string strUrl = "提供IP查询的网站的链接";
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq .GetResponse().GetResponseStream();
100米短跑技巧 StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
int i = all.IndexOf("[") + 1;
//分析字符串得到IP
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq .GetResponse().GetResponseStream();
100米短跑技巧 StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
int i = all.IndexOf("[") + 1;
//分析字符串得到IP
return ip;
/*
王珂微博 我用的是www.ip/getip.php?action=getip&ip_url=&from=web
(这种链接很容易的,百度“IP”得到一些网站,分析一下网站的链接就能得到)
返回的数据是:
<div class="well"><p>当前 IP:<code>0.0.0.0</code> 来自:XX省XX市 电信</p><p>GeoIP: Beijing, China</p></div>
解析这段就行
*/
4、//由于用到了ManagementClass、ManagementObjectCollection;必须添加引用System.Management.dll及using System.Management;
/*
王珂微博 我用的是www.ip/getip.php?action=getip&ip_url=&from=web
(这种链接很容易的,百度“IP”得到一些网站,分析一下网站的链接就能得到)
返回的数据是:
<div class="well"><p>当前 IP:<code>0.0.0.0</code> 来自:XX省XX市 电信</p><p>GeoIP: Beijing, China</p></div>
解析这段就行
*/
4、//由于用到了ManagementClass、ManagementObjectCollection;必须添加引用System.Management.dll及using System.Management;
[csharp] view plaincopyprint?private void GetIP2()
{
string stringMAC = "";
string stringIP = "";
{
string stringMAC = "";
string stringIP = "";
ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach(ManagementObject managementObject in managementObjectCollection)
{
if ((bool)managementObject["IPEnabled"] == true)
{
stringMAC += managementObject["MACAddress"].ToString();
string[] IPAddresses = (string[])managementObject["IPAddress"];
if (IPAddresses.Length > 0)
{
stringIP = IPAddresses[0];
}
}
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach(ManagementObject managementObject in managementObjectCollection)
{
if ((bool)managementObject["IPEnabled"] == true)
{
stringMAC += managementObject["MACAddress"].ToString();
string[] IPAddresses = (string[])managementObject["IPAddress"];
if (IPAddresses.Length > 0)
{
stringIP = IPAddresses[0];
}
}
}
txtMAC.Text = stringMAC.ToString();
txtIP.Text = stringIP.ToString();
}
txtMAC.Text = stringMAC.ToString();
txtIP.Text = stringIP.ToString();
}
private void GetIP2()
{
string stringMAC = "";
string stringIP = "";
ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach(ManagementObject managementObject in managementObjectCollection)
{
if ((bool)managementObject["IPEnabled"] == true)
{
string stringMAC = "";
string stringIP = "";
ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach(ManagementObject managementObject in managementObjectCollection)
{
if ((bool)managementObject["IPEnabled"] == true)
{
stringMAC += managementObject["MACAddress"].ToString();
string[] IPAddresses = (string[])managementObject["IPAddress"];
if (IPAddresses.Length > 0)
{
stringIP = IPAddresses[0];
}
}
}
txtMAC.Text = stringMAC.ToString();
txtIP.Text = stringIP.ToString();
}
stringMAC += managementObject["MACAddress"].ToString();
string[] IPAddresses = (string[])managementObject["IPAddress"];
if (IPAddresses.Length > 0)
{
stringIP = IPAddresses[0];
}
}
}
txtMAC.Text = stringMAC.ToString();
txtIP.Text = stringIP.ToString();
}
5、通过获取CMD里ipconfig命令的结果来得到IP
[csharp]
private void GetIP6()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
//重定向标准输出
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
//cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思
/*
收集一下 有备无患
关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
{
Process cmd = new Process();
cmd.StartInfo.FileName = "";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
//重定向标准输出
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
//cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思
/*
收集一下 有备无患
关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow到窗口后再ShowWindow
*/
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
textBox1.AppendText(info);
}
*/
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
textBox1.AppendText(info);
}
private void GetIP6()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
//重定向标准输出
{
Process cmd = new Process();
cmd.StartInfo.FileName = "";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
//重定向标准输出
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
//cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思
/*
收集一下 有备无患
关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
李天一判决 hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow到窗口后再ShowWindow
*/
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
//cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思
/*
收集一下 有备无患
关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
李天一判决 hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow到窗口后再ShowWindow
*/
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
textBox1.AppendText(info);
}
textBox1.AppendText(info);
}
存货跌价准备6、NetworkInformation
[csharp]
private void GetIP5()
{
//需要的命名空间
//using System.Net.NetworkInformation;
//using System.Net.Sockets;
string str = "";
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
int i = 0;
foreach (NetworkInterface adapter in adapters)
private void GetIP5()
{
//需要的命名空间
//using System.Net.NetworkInformation;
//using System.Net.Sockets;
string str = "";
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
int i = 0;
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection allAddress =
adapterProperties.UnicastAddresses;
if (allAddress.Count > 0)
{
str += "interface " + i + "description:\n\t " + adapter.Description + "\n ";
i++;
foreach (UnicastIPAddressInformation addr in allAddress)
{
if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6)
万维网万惠 {
ipListComb.Items.Add(addr.Address);
}
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection allAddress =
adapterProperties.UnicastAddresses;
if (allAddress.Count > 0)
{
str += "interface " + i + "description:\n\t " + adapter.Description + "\n ";
i++;
foreach (UnicastIPAddressInformation addr in allAddress)
{
if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6)
万维网万惠 {
ipListComb.Items.Add(addr.Address);
}
if (addr.Address.AddressFamily == AddressFamily.InterNetwork)
{
comboBox1.Items.Add(addr.Address);
}
}
}
}
MessageBox.Show(str);
}
{
comboBox1.Items.Add(addr.Address);
}
}
}
}
MessageBox.Show(str);
}
发布评论