Best way to programmatically configure network adapters in .NET

后端 未结 5 643
梦毁少年i
梦毁少年i 2020-12-01 11:18

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

5条回答
  •  广开言路
    2020-12-01 11:26

    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

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

    To set it to dhcp you'd use

    netsh interface ip set address "Local Area Connection" dhcp
    

    To do it from C# would be

    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.

提交回复
热议问题