C#获取本地IP的四种方式示例详解
作者:Koalin 时间:2023-04-16 00:52:50
1.第一种方式
采用System.Net.Dns的GetHostAddress的方式,具体请看代码:
/// <summary>
/// 网络不通畅可以获取
/// 不过能获取到具体的IP
/// </summary>
/// <returns></returns>
public static List<IPAddress> GetByGetHostAddresses()
{
try
{
IPAddress[] adds = Dns.GetHostAddresses(Dns.GetHostName());
return adds == null || adds.Length == 0 ? new List<IPAddress>() : adds.ToList<IPAddress>();
}
catch (Exception)
{
return new List<IPAddress>();
}
}
这种方式受到网络的影响,如果没有连接到网络,本地配置的部分IP是获取不到的,我也遇到一种情况是,电脑环境正常,就是获取不到,原因至今还不知道;
2.第二种方式
采用System.Management.ManagementClass来获取,详细请看代码:
/// <summary>
/// 只有网络通畅才能获取
/// </summary>
/// <returns></returns>
public static List<IPAddress> GetByManagementClass()
{
try
{
ManagementClass mClass = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection managementObjectCollection = mClass.GetInstances();
List<IPAddress> ls = new List<IPAddress>();
foreach (var item in managementObjectCollection)
{
if ((bool)item["IPEnabled"] == true)
{
foreach (var ip in (string[])item["IPAddress"])
{
IPAddress ipout = null;
IPAddress.TryParse(ip, out ipout);
if (ipout != null)
{
ls.Add(ipout);
}
}
}
}
return ls;
}
catch (Exception)
{
return new List<IPAddress>();
}
}
同样的这种方式也受到网络的约束,没有联网的状态下不一定能够获取到IP;
3.第三种方式
我们平时在命令行中输入ipconfig命令同样也是能获取,在程序中启动Ipconfig应用程序,然后解析出来,也是可以获取得到IP,详细请看代码:
public static List<IPAddress> GetByCMD()
{
try
{
Process cmd = new Process();
cmd.StartInfo.FileName = "ipconfig.exe";
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;
cmd.Start();
string info = "";
List<IPAddress> ls = new List<IPAddress>();
// info = cmd.StandardOutput.ReadToEnd();
Regex validipregex = new Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}");
//new Regex(@"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
while ((info = cmd.StandardOutput.ReadLine()) != null)
{
IPAddress ip = null;
Console.WriteLine(info);
info = validipregex.Match(info).Value;
IPAddress.TryParse(info, out ip);
if (ip != null)
{
ls.Add(ip);
}
}
cmd.WaitForExit();
cmd.Close();
return ls;
}
catch (Exception)
{
return new List<IPAddress>();
}
}
即便是通过这种方式来获取IP,如果在本机电脑没有联网的状态下,也是获取不到IP的,并且也不太建议使用这种方式;
4.第四种方法
采用NetworkInterface.GetAllNetworkInterfaces的方式是不受网络的影响的,联网或者不联网都能够获取到IP,详细请看代码:
/// <summary>
/// 无论网络通不通都能获取到Ip
/// </summary>
/// <returns></returns>
public static List<IPAddress> GetByNetworkInterface()
{
try
{
NetworkInterface[] intf = NetworkInterface.GetAllNetworkInterfaces();
List<IPAddress> ls = new List<IPAddress>();
foreach (var item in intf)
{
IPInterfaceProperties adapterPropertis = item.GetIPProperties();
UnicastIPAddressInformationCollection coll = adapterPropertis.UnicastAddresses;
foreach (var col in coll)
{
ls.Add(col.Address);
}
}
return ls;
}
catch (Exception)
{
return new List<IPAddress>();
}
}
以上所说的联网,包括连接在局域网中。
希望给有需要的朋友们带来帮助;
来源:https://www.cnblogs.com/hglSV/p/10759468.html
标签:C#,IP,地址
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现摄像头截图功能
2023-12-01 19:53:22
Android编程实现XML解析与保存的三种方法详解
2022-04-15 00:39:01
![](https://img.aspxhome.com/file/2023/5/125365_0s.png)
关于springboot2整合lettuce启动卡住问题的解决方法
2022-08-24 09:29:16
Flutter Flow实现滑动显隐层示例详解
2022-01-29 23:50:56
Java编程实现帕斯卡三角形代码示例
2023-11-02 08:08:24
![](https://img.aspxhome.com/file/2023/3/58303_0s.gif)
Android Rsa数据加解密的介绍与使用示例
2023-06-24 04:51:38
![](https://img.aspxhome.com/file/2023/3/94973_0s.png)
springboot中使用rabbitt的详细方法
2023-06-17 09:57:43
Java中this,static,final,const用法详解
2022-01-23 17:21:41
SpringBoot使用protobuf格式的接口方式
2022-02-14 14:20:17
![](https://img.aspxhome.com/file/2023/0/70950_0s.png)
Flutter自定义圆盘取色器
2023-07-05 23:55:43
![](https://img.aspxhome.com/file/2023/0/96010_0s.gif)
java创建线程的两种方法区别
2023-11-11 09:17:52
Android实现随手指移动小球
2021-06-03 23:38:46
![](https://img.aspxhome.com/file/2023/9/138369_0s.jpg)
Eclipse git推送上传错误问题解决方案
2022-07-19 15:19:39
![](https://img.aspxhome.com/file/2023/0/112050_0s.png)
简单实现Android学生管理系统(附源码)
2022-06-12 05:59:04
![](https://img.aspxhome.com/file/2023/0/137430_0s.png)
Java反射 PropertyDescriptor类案例详解
2022-03-13 14:27:32
Java实现AWT四大事件的详细过程
2023-11-28 18:39:52
![](https://img.aspxhome.com/file/2023/6/60076_0s.png)
mybatis中的count()按条件查询方式
2022-06-30 06:04:29
利用Distinct()内置方法对List集合的去重问题详解
2023-01-31 00:45:30
Java实现图片倒影的源码实例内容
2022-08-30 02:39:24
![](https://img.aspxhome.com/file/2023/2/65162_0s.jpg)
Java多线程synchronized同步方法详解
2023-06-30 00:39:42
![](https://img.aspxhome.com/file/2023/6/81226_0s.png)