发现在C#中的接口索引 [英] Finding the interface index in C#

查看:119
本文介绍了发现在C#中的接口索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个interace的指数在C#中的连接?

How can I get the index of an interace for a connection in C#?

如果指数是不是一个标准术语,我的意思是一个接口,相关联的数字,当你使用命令netsh的INT IPv4的表演诠释通过指数在左边列出你的连接,如。它也适用于添加路由[门户]面膜[指数]如果[接口索引]。

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]".

我知道这个名字和接口的描述,所以它是相当straightfoward使用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);
}

注意 GetIPv4Properties()可以返回如果没有IPv4的信息可用于接口。

Note that GetIPv4Properties() can return null if no IPv4 information is available for the interface.

这<一href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getipproperties.aspx"相对=nofollow> MSDN页面显示,可能是有帮助的例子。

This msdn page shows an example that might be helpful.

这篇关于发现在C#中的接口索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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