以编程方式配置.NET网络适配器最好的方法 [英] Best way to programmatically configure network adapters in .NET

查看:168
本文介绍了以编程方式配置.NET网络适配器最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有写在需要能够配置网络适配器在Windows C#应用程序。我有这个基本上通过WMI工作,但也有几件事情我不喜欢这种解决办法:有时设置似乎没有坚持,当网络电缆没有插好,从WMI返回错误方法,所以我不能告诉他们是否真的成功与否。

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.

我需要能够通过网络连接配置所有的可用设置 - 属性 - TCP / IP屏幕

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?

推荐答案

您可以使用进程来断火的的netsh 命令来设置在网络对话框中的所有属性。

You could use Process to fire off netsh commands to set all the properties in the network dialogs.

例如: 要在适配器设置一个静态ip地址

eg: To set a static ipaddress on an adapter

netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1

要设置为DHCP你会使用

To set it to dhcp you'd use

netsh interface ip set address "Local Area Connection" dhcp

要在C#这样做将是

Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1");
p.StartInfo = psi;
p.Start();

设置为静态可以好好几秒钟即可完成,所以如果你需要,请确保您等待进程退出。

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.

这篇关于以编程方式配置.NET网络适配器最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆