在 C# 中查找接口索引

Finding the interface index in C#(在 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# 中查找接口索引

基础教程推荐