none
如何實現本機網路訊息 RRS feed

  • 問題

  • 在VB2005中  請問要如何取得本機預設 GATEWAY 和MAC位址?

     

    在XP CMD模式下  輸入ipconfig /all  可以取的本機上相關的網路資訊

    不知道這功能可以在VB2005上實現嗎(覺得命令模式介面醜醜的,想自己做一各)

    本一為MY.computer底下有相關套件可以直接秀出

    但找了一下好像都沒有

    希望有經驗的大大 提供方向

     

     

    2007年7月9日 上午 02:39

解答

  • 範例程式(抱歉,只有 C#): 

    Code Snippet

    public static void ShowNetworkInterfaces()
    {
        IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        Console.WriteLine("Interface information for {0}.{1}     ",
                computerProperties.HostName, computerProperties.DomainName);
        if (nics == null || nics.Length < 1)
        {
            Console.WriteLine("  No network interfaces found.");
            return;
        }
                        
        Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
        foreach (NetworkInterface adapter in nics)
        {
            IPInterfaceProperties properties = adapter.GetIPProperties(); //  .GetIPInterfaceProperties();
            Console.WriteLine();
            Console.WriteLine(adapter.Description);
            Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
            Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
            Console.Write("  Physical address ........................ : ");
            PhysicalAddress address = adapter.GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
            for(int i = 0; i< bytes.Length; i++)
            {
                // Display the physical address in hexadecimal.
                Console.Write("{0}", bytes[i].ToString("X2"));
                // Insert a hyphen after each byte, unless we are at the end of the
                // address.
                if (i != bytes.Length -1)
                {
                     Console.Write("-");
                }
            }
            Console.WriteLine();
        }

     

     

    取得 Gateway 的 IP (VB):

     

    Code Snippet

    Public Shared Sub DisplayGatewayAddresses()
        Console.WriteLine("Gateways")
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        For Each adapter In  adapters
            Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()
            Dim addresses As GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses
            If addresses.Count > 0 Then
                Console.WriteLine(adapter.Description)
                Dim address As GatewayIPAddressInformation
                For Each address In  addresses
                    Console.WriteLine("  Gateway Address ......................... : {0}", address.ToString())
                Next address
                Console.WriteLine()
            End If
        Next adapter

    End Sub 'DisplayGatewayAddresses

     

     

    2007年7月9日 上午 02:58
    版主
  • 先看本版公告裡面介紹的微軟官方範例 VB2005 的 sample 101 ,安裝以後,找到範例目錄:

    NetworkInfo

     

    執行裡面的東西對照,裡面的東西滿多的,包含網卡速度都有抓。

     

    還有一個非同步的 ping 按鈕可以測試。

    2007年7月9日 上午 04:04
    版主

所有回覆

  • 範例程式(抱歉,只有 C#): 

    Code Snippet

    public static void ShowNetworkInterfaces()
    {
        IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        Console.WriteLine("Interface information for {0}.{1}     ",
                computerProperties.HostName, computerProperties.DomainName);
        if (nics == null || nics.Length < 1)
        {
            Console.WriteLine("  No network interfaces found.");
            return;
        }
                        
        Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
        foreach (NetworkInterface adapter in nics)
        {
            IPInterfaceProperties properties = adapter.GetIPProperties(); //  .GetIPInterfaceProperties();
            Console.WriteLine();
            Console.WriteLine(adapter.Description);
            Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
            Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
            Console.Write("  Physical address ........................ : ");
            PhysicalAddress address = adapter.GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
            for(int i = 0; i< bytes.Length; i++)
            {
                // Display the physical address in hexadecimal.
                Console.Write("{0}", bytes[i].ToString("X2"));
                // Insert a hyphen after each byte, unless we are at the end of the
                // address.
                if (i != bytes.Length -1)
                {
                     Console.Write("-");
                }
            }
            Console.WriteLine();
        }

     

     

    取得 Gateway 的 IP (VB):

     

    Code Snippet

    Public Shared Sub DisplayGatewayAddresses()
        Console.WriteLine("Gateways")
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        For Each adapter In  adapters
            Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()
            Dim addresses As GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses
            If addresses.Count > 0 Then
                Console.WriteLine(adapter.Description)
                Dim address As GatewayIPAddressInformation
                For Each address In  addresses
                    Console.WriteLine("  Gateway Address ......................... : {0}", address.ToString())
                Next address
                Console.WriteLine()
            End If
        Next adapter

    End Sub 'DisplayGatewayAddresses

     

     

    2007年7月9日 上午 02:58
    版主
  • 先看本版公告裡面介紹的微軟官方範例 VB2005 的 sample 101 ,安裝以後,找到範例目錄:

    NetworkInfo

     

    執行裡面的東西對照,裡面的東西滿多的,包含網卡速度都有抓。

     

    還有一個非同步的 ping 按鈕可以測試。

    2007年7月9日 上午 04:04
    版主
  • 感謝大大  小第去研究看看了

     

    2007年7月9日 上午 04:18