Finding the interface index in C#(在 C# 中查找接口索引)
问题描述
如何在 C# 中获取连接的接口索引?
How can I get the index of an interace for a connection in C#?
如果索引不是一个标准术语,我指的是与接口关联的数字,例如当您使用命令netsh int ipv4 show int"在左侧按索引列出您的连接时.也用于route add [gateway] mask [index] if [interface index]"中.
In case index isn't a standard term, I mean the number associated with an interface, such as when you use the command "netsh int ipv4 show int" to list your connections by index on the left. It's also used in "route add [gateway] mask [index] if [interface index]".
我知道接口的名称和描述,因此使用 NetworkInterface.GetAllNetworkInterfaces() 然后在那里找到正确的接口是相当简单的.但是从那里,我找不到索引.我认为 ID 可能相同,但它具有{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"形式的值,而不是小整数值.
I know the name and description of the interface, so it's fairly straightfoward to use NetworkInterface.GetAllNetworkInterfaces() and then find the right one there. From there though, I can't find the index. I thought ID might be the same, but that has values of the form "{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", not small integer values.
推荐答案
我想你想要
myInterface.GetIPProperties().GetIPv4Properties().Index;
如
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach(var @interface in interfaces)
{
var ipv4Properties = @interface.GetIPProperties().GetIPv4Properties();
if (ipv4Properties != null)
Console.WriteLine(ipv4Properties.Index);
}
请注意,如果接口没有可用的 IPv4 信息,GetIPv4Properties()
可以返回 null
.
Note that GetIPv4Properties()
can return null
if no IPv4 information is available for the interface.
此 msdn 页面显示一个可能有用的例子.
This msdn page shows an example that might be helpful.
这篇关于在 C# 中查找接口索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中查找接口索引
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01