C# 通过 ManagementClass 获取本机IP 地址 报错
2014-07-21 17:57
323 查看
程序中有IP地址限制的功能,需要获取本机的IP 地址信息。通过 ManagementClass 类来获取本机的IP 地址信息。代码如下:
但是在某些电脑上报错,显示的错误也为空。电脑上的【网络连接】也找不到。
原因:电脑上的 Network Connections 服务未启动。
解决方法:我的电脑——右键——管理——服务和应用程序——服务——启动 Network Connections
重启电脑再运行程序即可。
public static System.Collections.Generic.List<IPIdentity> GetLocalIPAdressList() { string ip = string.Empty; string subNet = string.Empty; System.Collections.Generic.List<IPIdentity> list = new System.Collections.Generic.List<IPIdentity>(); ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances(); foreach (ManagementObject obj2 in instances) { if (System.Convert.ToBoolean(obj2.get_Item("ipEnabled"))) { ip = (obj2.get_Item("IPAddress") as string[])[0]; subNet = (obj2.get_Item("IPSubnet") as string[])[0]; if (!(string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(subNet))) { IPIdentity identity = new IPIdentity(ip, subNet); list.Add(identity); } } } return list; }
但是在某些电脑上报错,显示的错误也为空。电脑上的【网络连接】也找不到。
原因:电脑上的 Network Connections 服务未启动。
解决方法:我的电脑——右键——管理——服务和应用程序——服务——启动 Network Connections
重启电脑再运行程序即可。
相关文章推荐
- C#通过ARP获取指定IP的MAC地址
- C#通过ARP获取指定IP的MAC地址
- C#中使用ManagementClass获取本机信息
- C#编程之 如何获取本地和远程主机的IP及MAC地址
- C#获取本地计算机名IP,Mac地址
- 通过IP或socket获取对方的MAC地址
- C#中获取本地计算机的的计算机名,IP和MAC地址
- C#获取IP及MAC地址
- C# 获取IP和MAC地址
- C# 获取局域网IP和MAC地址
- C#获取本地计算机名,IP,MAC(物理)地址
- C# 获取 MAC 地址 IP 地址 最简单的
- C#获取本地计算机名,IP,MAC(物理)地址(转)
- C#获取本地计算机名IP,Mac地址
- C#获取本地计算机名IP,Mac地址
- C#获取本地计算机名IP,Mac地址
- C#/.NET获取本机的外网IP地址的简单方法
- C#获取IP及MAC地址
- 【20090903-01】C#中使用ManagementClass获取本机信息
- C#中通过IP获取MAC地址,获取本机IP和MAC地址