有没有办法指定将使用哪个NetworkInterface .net? [英] Is there a way to specify which NetworkInterface .net will use?

查看:84
本文介绍了有没有办法指定将使用哪个NetworkInterface .net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Wifi和LAN连接在设备上进行一系列测试.我有两个网络接口卡,一个是WiFi,另一个是有线的.该设备具有可通过两个网络接口访问的单个IP地址.有没有办法保证我的机器正在使用特定的网络接口,以便可以通过有线连接运行所有测试,然后通过Wifi连接运行所有测试?

I would like to run a series of tests on a device using its Wifi and LAN connections. I have two network interface cards, one is WiFi, the other is wired. The device has a single IP address that is accessible through both Network Interfaces. Is there a way to guarantee that my machine is using a particular Network Interface so that I can run all tests through the wired connection, and then run all tests through the Wifi connection?

请记住,两个连接共享相同的子网,因此任何一个都将成功连接到设备.我只希望能够来回切换并指定在任何给定时间使用的连接.

Keep in mind, both connections share the same subnet, so either will successfully connect to the device. I just want to be able to switch back and forth and specify which connection I'm using at any given time.

我正在使用C#在.net上使用HttpClient.有没有一种方法可以强制HttpClient使用特定的网络接口?似乎可以根据IP地址自动选择一个,但是在这种情况下,有2个网络接口可以工作.我需要能够以编程方式来回切换以运行自动化测试.

I'm using HttpClient on .net with C#. Is there a way to force HttpClient to use a particular Network Interface? It seems to automatically choose one based on IP Address, but in this case, there are 2 Network Interfaces that can work. I need to be able to programatically switch back and forth to run my automated tests.

我注意到一个叫做接口度量"的东西,可以通过IP4属性高级TCP/IP设置手动更改.每次都会使用编号较小的接口吗?有没有办法以编程方式更改此值?

I have noticed something called an 'Interface Metric' that can be changed manually via IP4 Properties Advanced TCP/IP Settings. Will the smaller numbered interface be used every time? Is there a way to change this value programatically?

更多说明:我之所以没有直接跳到来回禁用WiFi NIC或有线NIC的原因是,当您禁用WiFi NIC时,您断开了它所连接的设备,并且在以下情况下它不会自动重新连接您启用它.(即使您说自动连接",重新启用后也不会连接).但是,我找到了一个快速的解决方案.将有线NIC上的接口指标设置为低于WiFi NIC上的接口指标.然后,只需启用和禁用有线NIC.禁用后,系统将使用WiFi NIC.启用后,系统将使用有线网卡,因为其接口指标设置得较低.现在,我只需要弄清楚如何以编程方式启用/禁用有线网卡...

MORE CLARIFICATION: The reason I didn't directly jump to Disabling the WiFi NIC or the Wired NIC back and forth was when you disable the WiFi NIC, you disconnect the device it was connected to and it doesn't automatically reconnect when you enable it. (even when you say 'connect automatically' it doesn't connect when re-enabled). However, I found a quick solution. Set the Interface Metric on the Wired NIC to be lower than the Interface Metric on the WiFi NIC. Then, just enable and disable the Wired NIC. When disabled, the WiFi NIC will be used by the system. When enabled, the Wired NIC will be used by the system because its Interface Metric is set lower. Now, I just need to figure out how to programatically enable/disable the Wired NIC...

推荐答案

我最终通过将有线NIC上的Interface Metric设置为低于Wifi NIC上的Interface Metric来解决了这个问题.这样,我能够简单地禁用和启用有线NIC,这将有效地更改测试期间随时使用的NIC.要禁用NIC,我将netsh.exe命令与以下代码一起使用.取决于我是打开还是关闭它,命令"是启用"还是禁用":

I ended up solving this by setting the Interface Metric on the wired NIC to be lower than the Interface Metric on the Wifi NIC. In this way, I was able to simply disable and enable the Wired NIC which would effectively change which NIC was being used at any time during the test. To Disable the NIC, I use the netsh.exe command with the following code. The 'command' was either 'enable' or 'disable' depending if I was turning it on or off:

        System.Diagnostics.Process p = new System.Diagnostics.Process
        {
            StartInfo =
            {
                FileName = "netsh.exe",
                Arguments = $"interface set interface \"{nicName}\" {command}",
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };

        bool started = p.Start();

这篇关于有没有办法指定将使用哪个NetworkInterface .net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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