简体   繁体   English

有没有办法指定将使用哪个 NetworkInterface .net?

[英]Is there a way to specify which NetworkInterface .net will use?

I would like to run a series of tests on a device using its Wifi and LAN connections.我想在使用 Wifi 和 LAN 连接的设备上运行一系列测试。 I have two network interface cards, one is WiFi, the other is wired.我有两张网卡,一张是wifi,一张是有线的。 The device has a single IP address that is accessible through both Network Interfaces.该设备具有可通过两个网络接口访问的单个 IP 地址。 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?有没有办法保证我的机器使用特定的网络接口,以便我可以通过有线连接运行所有测试,然后通过 Wifi 连接运行所有测试?

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.我只想能够来回切换并指定我在任何给定时间使用的连接。

I'm using HttpClient on .net with C#.我在 .net 上用 C# 使用 HttpClient。 Is there a way to force HttpClient to use a particular Network Interface?有没有办法强制 HttpClient 使用特定的网络接口? It seems to automatically choose one based on IP Address, but in this case, there are 2 Network Interfaces that can work.它似乎会根据 IP 地址自动选择一个,但在这种情况下,有 2 个网络接口可以工作。 I need to be able to programatically switch back and forth to run my automated tests.我需要能够以编程方式来回切换以运行我的自动化测试。

I have noticed something called an 'Interface Metric' that can be changed manually via IP4 Properties Advanced TCP/IP Settings.我注意到一种叫做“接口指标”的东西,可以通过 IP4 属性高级 TCP/IP 设置手动更改。 Will the smaller numbered interface be used every time?每次都会使用编号较小的接口吗? Is there a way to change this value programatically?有没有办法以编程方式更改此值?

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.更多说明:我没有直接跳到禁用 WiFi NIC 或有线 NIC 来回的原因是当您禁用 WiFi NIC 时,您断开了它所连接的设备,并且在您启用它时它不会自动重新连接. (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.将有线 NIC 上的接口指标设置为低于 WiFi NIC 上的接口指标。 Then, just enable and disable the Wired NIC.然后,只需启用和禁用有线 NIC。 When disabled, the WiFi NIC will be used by the system.禁用时,系统将使用 WiFi 网卡。 When enabled, the Wired NIC will be used by the system because its Interface Metric is set lower.启用后,系统将使用有线 NIC,因为其接口指标设置得较低。 Now, I just need to figure out how to programatically enable/disable the Wired NIC...现在,我只需要弄清楚如何以编程方式启用/禁用有线网卡...

One way would be to update your route table to force traffic for a particular IP onto a particular NIC.一种方法是更新路由表以将特定 IP 的流量强制到特定 NIC 上。 The route table is what is used to decide which NIC to send traffic out on.路由表用于决定将流量发送到哪个 NIC。

You could do this in a couple of ways:您可以通过以下几种方式执行此操作:

1) Use P/Invoke to update the route table with C# code (see here for a good example , but basically you are calling CreateIpForwardEntry ). 1) 使用 P/Invoke 使用 C# 代码更新路由表(请参阅此处的一个很好的示例,但基本上您正在调用CreateIpForwardEntry )。

2) Use Process.Start to call route.exe, eg 2)使用Process.Start调用route.exe,例如

route.exe ADD destination_network MASK subnet_mask  gateway_ip metric_cost

and

route.exe CHANGE destination_network MASK subnet_mask  gateway_ip metric_cost

You can use the library WlanAPI.cs您可以使用库 WlanAPI.cs

static public void Connect_to_wifi(string ssid)
            {
                WlanClient client = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    // Lists all networks with WEP security
                    //Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                    //foreach (Wlan.WlanAvailableNetwork network in networks)
                    //{
                    //    if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP)
                    //    {
                    //        Console.WriteLine("Found WEP network with SSID {0}.", network.profileName);
                    //    }
                    //}
    
                    // Retrieves XML configurations of existing profiles.
                    // This can assist you in constructing your own XML configuration
                    // (that is, it will give you an example to follow).
                    foreach (Wlan.WlanProfileInfo profileInfo in wlanIface.GetProfiles())
                    {
                        if (profileInfo.profileName == ssid)
                        {
                            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, wlanIface.GetProfileXml(profileInfo.profileName), true);
                            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileInfo.profileName);
                            break;
                        }
                    }
                }
            }

Best way is to use WinSock bind: TCP/IP connection on a specific interface最好的方法是使用 WinSock bind: TCP/IP connection on a specific interface

But once you have single IP for bith NICs then:但是一旦你有一个用于比特网卡的单一 IP,那么:

Disable NIC from PowerShell: https://docs.microsoft.com/en-us/powershell/module/netadapter/disable-netadapter?view=win10-ps从 PowerShell 禁用 NIC: https : //docs.microsoft.com/en-us/powershell/module/netadapter/disable-netadapter? view = win10- ps

Calling PowerShell from c#: https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/从 C# 调用 PowerShell: https : //blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

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.我最终通过将有线 NIC 上的接口指标设置为低于 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.要禁用 NIC,我使用带有以下代码的 netsh.exe 命令。 The 'command' was either 'enable' or 'disable' depending if I was turning it on or off: 'command' 是 'enable' 或 'disable' 取决于我是打开还是关闭它:

        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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何指定在运行时使用哪个.NET框架 - How to specify which .NET framework to use at runtime 使用log4net,您是否指定要使用的附加程序? - With log4net, do you specify which appender to use? 有没有一种方法可以指定在网站范围内使用哪个字符串比较? - Is there a way to specify which string comparison to use on a site-wide level? 指定在.NET中用于解析主机名的DNS服务器 - Specify which DNS servers to use to resolve hostnames in .NET 如何指定在GridView RowUpdate中使用哪个控件 - How to specify which control to use in the gridview rowupdate PRISM + MEF - 如何指定要使用的导出? - PRISM + MEF — How to specify which export to use? 指定用户可以在文本框中输入数据的方式 - Specify the way which the user can enter the data in text box .NET:您可以手动指定要包含在自动生成的WSDL中的类型吗? - .NET: Can you manually specify which types to include in autogenerated WSDL? 有没有一种使用Json.Net来反序列化某些json的方法,该json调用了一种将项添加到集合中的方法(没有属性设置器) - Is there a way to use Json.Net to deserialize some json which calls a method to add the items to a collection (there is no property setter) 哪个是使用委托的正确方法? - Which is the right way to use delegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM