以编程方式检索 .NET 中断开连接的网络适配器信息
我有一个用 C# 编写的应用程序,需要从断开连接的网络适配器检索 IP 地址、子网掩码等信息。
我尝试过使用各种方法,例如 WMI 和 .NET NetworkAdapter 类,但当网络适配器断开连接时它们不会返回任何有用的数据。我非常确定 Windows 会将此信息保存在某处,因为我可以使用 netsh 应用网络设置,并且它会正确显示在控制面板中。
在 XP 中对我有用的一件事是解析 netsh 工具的输出,即使适配器已断开连接,它也会返回信息。但是,这似乎不适用于 Windows 7。
Win XP 输出:
Configuration for interface "Local Area Connection 5"
DHCP enabled: No
IP Address: 169.254.0.128
SubnetMask: 255.255.255.0
InterfaceMetric: 0
Win7 输出:
Configuration for interface "Local Area Connection 2"
DHCP enabled: No
InterfaceMetric: 5
有什么想法吗?
I have an application written in C# that needs to retrieve information like IP address, subnet mask from a disconnected network adapter.
I've tried using various methods such as WMI and the .NET NetworkAdapter class but they don't return any useful data when the network adapter is disconnected. I'm pretty sure Windows keeps this information somewhere, since I can apply network settings using netsh and it appears correctly in the Control Panel.
One thing that worked for me in XP was to parse the output of the netsh
tool and it would return information even for a disconnected adapter. However, this doesn't seem to work on Windows 7.
Win XP output:
Configuration for interface "Local Area Connection 5"
DHCP enabled: No
IP Address: 169.254.0.128
SubnetMask: 255.255.255.0
InterfaceMetric: 0
Win7 output:
Configuration for interface "Local Area Connection 2"
DHCP enabled: No
InterfaceMetric: 5
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
和/或
然后
如果 DHCP - ON 则仅 NetworkChange.NetworkAddressChanged 因为当前 IP 无法定义
and/or
and then
if DHCP - ON then only NetworkChange.NetworkAddressChanged because current IP is impossible to define