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# 中查找接口索引


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01