发现在C#中的接口索引索引、接口、现在

由网友(wirepuller 阴谋者 。)分享简介:我怎样才能得到一个interace的指数在C#中的连接?How can I get the index of an interace for a connection in C#?如果指数是不是一个标准术语,我的意思是一个接口,相关联的数字,当你使用命令netsh的INT IPv4的表演诠释通过指数在左边列出你的连...

我怎样才能得到一个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.

这 MSDN页面显示,可能是有帮助的例子。

This msdn page shows an example that might be helpful.

阅读全文

相关推荐

最新文章