【问题标题】:How do i use System.Net.NetworkInformation.GatewayIPAddressInformation Class?我如何使用 System.Net.NetworkInformation.GatewayIPAddressInformation 类?
【发布时间】:2010-10-01 14:39:05
【问题描述】:

我尝试了书中的每一个技巧,创建了一个新对象,实例化它(即使它说我不能),只是尝试创建一个引用来使用它,然后还尝试调用里面的 .Address 值它在使用它时就像我会使用一个共享成员(没有实例化)并且没有任何工作,msdn示例/帮助是无用的......我什至尝试继承它并像那样使用它,它们都没有工作,我确定我是缺少一些东西,谁能给我一些代码示例?这里是它上面的 msdn 文档给你一个概述......

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.gatewayipaddressinformation.aspx

虽然我更喜欢 vb.net,但我可以在 c# 和 vb.net 中阅读和编码,所以两者都可以。

谢谢:)

顺便说一句:如果有人想知道为什么,我只是想从电脑上获取我的路由器名称/标签,如下所示...How to get Router Name & IP as shown in Windows Network Tab? (in Code)

【问题讨论】:

    标签: c# .net asp.net asp.net-mvc vb.net


    【解决方案1】:

    来自MSDN

    • 简而言之,您可以致电NetworkInterface.GetAllNetworkInterfaces() 获取一组适配器。
    • 在其中一个适配器上,获取adapter.GetIPProperties().GatewayAddresses
    • GatewayAddresses 的每个元素都是一个GatewayIPAddressInformation

    public static void DisplayGatewayAddresses()
    {
        Console.WriteLine("Gateways");
        NetworkInterface[] adapters  = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in adapters)
        {
            IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
            GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
            if (addresses.Count >0)
            {
                Console.WriteLine(adapter.Description);
                foreach (GatewayIPAddressInformation address in addresses)
                {
                    Console.WriteLine("  Gateway Address ......................... : {0}", 
                        address.Address.ToString());
                }
                Console.WriteLine();
            }
        }
    }
    

    【讨论】:

    • 啊,谢谢,我一直在循环这个实际上没有意识到类型(感谢隐式声明哈哈)寻找我过去 3 天一直在寻找的难以捉摸的“路由器名称” ...谢谢...
    • ps:我正在寻找如何获取基本/用户 SOHO 路由器的路由器名称,但在 networkInterface 类中找不到它...stackoverflow.com/questions/3839189/…
    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2011-06-12
    • 2011-07-10
    • 2014-01-01
    相关资源
    最近更新 更多