在 .NET 中以编程方式配置网络适配器的最佳方法
我有一个用 C# 编写的应用程序,需要能够在 Windows 中配置网络适配器。 我基本上通过 WMI 进行工作,但该解决方案有一些我不喜欢的地方:有时设置似乎不固定,并且当未插入网络电缆时,从 WMI 返回错误方法,所以我无法判断他们是否真的成功了。
我需要能够通过网络连接 - 属性 - TCP/IP 屏幕配置所有可用的设置。
最好的方法是什么?
I have an application written in C# that needs to be able to configure the network adapters in Windows. I have this basically working through WMI, but there are a couple of things I don't like about that solution: sometimes the settings don't seem to stick, and when the network cable is not plugged in, errors are returned from the WMI methods, so I can't tell if they really succeeded or not.
I need to be able to configure all of the settings available through the network connections - Properties - TCP/IP screens.
What's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Process
触发 netsh 命令来设置所有属性在网络对话框中。例如:
要在适配器上设置静态 ip 地址
要将其设置为 dhcp,您可以使用
从 C# 执行此操作 将
设置为静态 可能需要几秒钟才能完成,因此如果需要,请确保等待该过程出口。
You could use
Process
to fire off netsh commands to set all the properties in the network dialogs.eg:
To set a static ipaddress on an adapter
To set it to dhcp you'd use
To do it from C# would be
Setting to static can take a good couple of seconds to complete so if you need to, make sure you wait for the process to exit.
用我的代码
SetIpAddress 和 SetDHCP
With my code
SetIpAddress and SetDHCP
在 @PaulB 的回答帮助下
with the help of @PaulB's answers help
我可以告诉你木马的做法,在清理了几次之后,就是在 HKEY_LOCAL_MACHINE 下设置注册表项。 他们设置的主要方法是 DNS 方法,这种方法绝对有效,任何曾经被感染并且无法再访问 windowsupdate.com、mcafee.com 等的人都可以证明这一点。
I can tell you the way the trojans do it, after having had to clean up after a few of them, is to set registry keys under HKEY_LOCAL_MACHINE. The main ones they set are the DNS ones and that approach definitely sticks which can be attested to by anyone who has ever been infected and can no longer get to windowsupdate.com, mcafee.com, etc.
看看这个应用程序。 这是一个完整的应用程序,用于设置 wifi 和以太网 ip
https://github.com/kamran7679/ConfigureIP
Checkout this app. it is a complete application to set both wifi and ethernet ips
https://github.com/kamran7679/ConfigureIP