このエントリーをはてなブックマークに追加


ネットワークカードの各種情報を取得するサンプルコード

ネットワークアダプタ数、名称、詳細、スピード、ループバックであるかどうか、ネットワークアダプタがアップしているか
IPアドレス、マルチキャストアドレス、マスク、DHCPかどうか、DNSサーバ、ゲートウェイ情報を取得する
サンプルコードを以下に公開します。

動作確認環境

  • Windows 10 ver.21H2
  • Visual Studio 2022
  • .NET Framework 4.7.2
  • コンソールアプリケーション

ネットワークアダプタの情報取得のサンプルコード

以下に C#, Visual Basic(VB) によるサンプルコードを記します。

C#によるネットワークアダプタ情報取得サンプルコード

using System;
using System.Net;
using System.Net.NetworkInformation;

namespace NetIF
{
    internal class Program
    {
        static void Main(string[] args)
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            Console.WriteLine("Number of adapters: {0}\n", adapters.Length);
            foreach (NetworkInterface adapter in adapters)
            {
                Console.WriteLine("Name: {0}\n\tDescription: {1}\n\tSpeed: {2}"
                    , adapter.Name, adapter.Description, adapter.Speed);
                Console.WriteLine("\tloopback?: {0}"
                    , adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback ? "yes" : "no");
                Console.WriteLine("\tifup?: {0}"
                    , adapter.OperationalStatus == OperationalStatus.Up ? "yes" : "no");
                if (adapter.OperationalStatus == OperationalStatus.Up)
                {
                    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                    // dns
                    IPAddressCollection dns = adapterProperties.DnsAddresses;
                    if (dns != null)
                    {
                        foreach (IPAddress ip in dns)
                        {
                            Console.WriteLine("\t\tDNS: {0}", ip.ToString());
                        }
                    }
                    // anycast
                    IPAddressInformationCollection anycast = adapterProperties.AnycastAddresses;
                    if (anycast != null)
                    {
                        foreach (IPAddressInformation any in anycast)
                        {
                            Console.WriteLine("\t\tAnycast: {0}", any.Address.ToString());
                        }
                    }
                    // multicast
                    MulticastIPAddressInformationCollection multicast = adapterProperties.MulticastAddresses;
                    if (multicast != null)
                    {
                        foreach (MulticastIPAddressInformation multi in multicast)
                        {
                            Console.WriteLine("\t\tMulticast: {0}", multi.Address.ToString());
                        }
                    }
                    // unicast
                    UnicastIPAddressInformationCollection unicast = adapterProperties.UnicastAddresses;
                    if (unicast != null)
                    {
                        foreach (UnicastIPAddressInformation uni in unicast)
                        {
                            Console.WriteLine("\t\tUnicast Address:  {0}", uni.Address.ToString());
                            Console.WriteLine("\t\tUnicast IPv4Mask: {0}", uni.IPv4Mask.ToString());
                            Console.WriteLine("\t\tUnicast PrefixOrigin: {0}", uni.PrefixOrigin.ToString());
                            Console.WriteLine("\t\tUnicast SuffixOrigin: {0}", uni.SuffixOrigin.ToString());
                            Console.WriteLine("\t\tDHCP?: {0}", uni.PrefixOrigin == PrefixOrigin.Dhcp ? "yes" : "no");
                        }
                    }
                    // default gateway
                    GatewayIPAddressInformationCollection gateways = adapterProperties.GatewayAddresses;
                    if (gateways != null)
                    {
                        foreach (GatewayIPAddressInformation gateway in gateways)
                        {
                            Console.WriteLine("\t\tGateway Address: {0}", gateway.Address.ToString());
                        }
                    }
                }
            }
        }
    }
}

Visual Basicによるネットワークアダプタ情報取得サンプルコード

Imports System.Net
Imports System.Net.NetworkInformation

Module Module1

    Sub Main()
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Console.WriteLine("Number of adapters: {0}" & vbLf, adapters.Length)

        For Each adapter As NetworkInterface In adapters
            Console.WriteLine("Name: {0}" & vbLf & vbTab & "Description: {1}" & vbLf & vbTab & "Speed: {2}", adapter.Name, adapter.Description, adapter.Speed)
            Console.WriteLine(vbTab & "loopback?: {0}", If(adapter.NetworkInterfaceType = NetworkInterfaceType.Loopback, "yes", "no"))
            Console.WriteLine(vbTab & "ifup?: {0}", If(adapter.OperationalStatus = OperationalStatus.Up, "yes", "no"))

            If adapter.OperationalStatus = OperationalStatus.Up Then
                Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()
                Dim dns As IPAddressCollection = adapterProperties.DnsAddresses

                If dns IsNot Nothing Then

                    For Each ip As IPAddress In dns
                        Console.WriteLine(vbTab & vbTab & "DNS: {0}", ip.ToString())
                    Next
                End If

                Dim anycast As IPAddressInformationCollection = adapterProperties.AnycastAddresses

                If anycast IsNot Nothing Then

                    For Each any As IPAddressInformation In anycast
                        Console.WriteLine(vbTab & vbTab & "Anycast: {0}", any.Address.ToString())
                    Next
                End If

                Dim multicast As MulticastIPAddressInformationCollection = adapterProperties.MulticastAddresses

                If multicast IsNot Nothing Then

                    For Each multi As MulticastIPAddressInformation In multicast
                        Console.WriteLine(vbTab & vbTab & "Multicast: {0}", multi.Address.ToString())
                    Next
                End If

                Dim unicast As UnicastIPAddressInformationCollection = adapterProperties.UnicastAddresses

                If unicast IsNot Nothing Then

                    For Each uni As UnicastIPAddressInformation In unicast
                        Console.WriteLine(vbTab & vbTab & "Unicast Address:  {0}", uni.Address.ToString())
                        Console.WriteLine(vbTab & vbTab & "Unicast IPv4Mask: {0}", uni.IPv4Mask.ToString())
                        Console.WriteLine(vbTab & vbTab & "Unicast PrefixOrigin: {0}", uni.PrefixOrigin.ToString())
                        Console.WriteLine(vbTab & vbTab & "Unicast SuffixOrigin: {0}", uni.SuffixOrigin.ToString())
                        Console.WriteLine(vbTab & vbTab & "DHCP?: {0}", If(uni.PrefixOrigin = PrefixOrigin.Dhcp, "yes", "no"))
                    Next
                End If

                Dim gateways As GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses

                If gateways IsNot Nothing Then

                    For Each gateway As GatewayIPAddressInformation In gateways
                        Console.WriteLine(vbTab & vbTab & "Gateway Address: {0}", gateway.Address.ToString())
                    Next
                End If
            End If
        Next
    End Sub

End Module

以上、C#, Visual Basic をつかったネットワークアダプタの情報取得サンプルコードでした。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-09-19 (月) 17:01:30