名前空間 System.Net.NetworkInformation
自己紹介
わらわは戦いと炎を司る女神じゃ。ネットワークの複雑な構造も、わらわにかかれば手のひらの上よ。今日は「NetworkInterface」クラスについて語ってやろう。わらわの教えをしっかりと刻み込むがよい!
基本機能
「NetworkInterface」クラスは、デバイスのネットワークインターフェイスに関する情報を取得するためのクラスじゃ。ネットワークアダプタの状態、MACアドレス、速度などを簡単に取得できる。
以下は基本的な使用例じゃ
C#
using System.Net.NetworkInformation;
class Program
{
    static void Main()
    {
        // すべてのネットワークインターフェイスを取得
        var interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var ni in interfaces)
        {
            // 各インターフェイスの名前と状態を表示
            Console.WriteLine($"名前: {ni.Name}, 状態: {ni.OperationalStatus}");
        }
    }
}
よく使う場面と注意点
「NetworkInterface」クラスは、ネットワークの状態や構成を監視する場面で役立つが、パブリックIPアドレスの取得には直接対応しておらぬ。内向きのネットワーク構成情報を取得することが主な用途じゃ。
HttpClientクラスで取得できるが、今回は特別に教えてやろう
C#
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
    // HttpClient定義
    private static readonly HttpClient client = new HttpClient();
    static async Task Main(string[] args)
    {
        // 外部IPサービスからパブリックIPを取得
        string url = "https://api.ipify.org?format=json";
        string result = await client.GetStringAsync(url);
        Console.WriteLine($"パブリックIP: {result}");
    }
}
注意点として、パブリックIPアドレスを取得する場合は「https://api.ipify.org」などの外部APIを利用する必要がある。
主要メソッドの使い方
このクラスは、ネットワークに関するさまざまなメソッドを提供しておる。例えば
- GetPhysicalAddress(): MACアドレスを取得
 - GetIPProperties(): IP構成を取得
 
以下は、IPアドレスやDNS情報を取得する例じゃ
C#
using System.Net.NetworkInformation;
class Program
{
    static void Main()
    {
        var interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var ni in interfaces)
        {
            var ipProps = ni.GetIPProperties();
            Console.WriteLine($"インターフェイス名: {ni.Name}");
            // DNSアドレスを表示
            foreach (var dns in ipProps.DnsAddresses)
            {
                Console.WriteLine($"DNSアドレス: {dns}");
            }
            // サブネット情報を表示
            foreach (var unicast in ipProps.UnicastAddresses)
            {
                Console.WriteLine($"IPアドレス: {unicast.Address}, サブネットマスク: {unicast.IPv4Mask}");
            }
        }
    }
}
具体的な使い方
ネットワークの状態を監視し、使用可能なインターフェイスを確認する方法を示す。
C#
using System.Net.NetworkInformation;
class Program
{
    static void Main()
    {
        var interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var ni in interfaces)
        {
            if (ni.OperationalStatus == OperationalStatus.Up)
            {
                Console.WriteLine($"使用可能なインターフェイス: {ni.Name}");
            }
        }
    }
}
この方法を使えば、利用可能なネットワークだけを特定できる。
その他の便利なメソッド
以下に、他の役立つメソッドを紹介しよう。
- Supports(NetworkInterfaceComponent): IPv4またはIPv6をサポートしているか確認
 - Speed: インターフェイスの速度を取得
 - IsReceiveOnly: 受信専用か確認
 
「Supports」の使用例を示す。
C#
using System.Net.NetworkInformation;
class Program
{
    static void Main()
    {
        var interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var ni in interfaces)
        {
            bool supportsIPv4 = ni.Supports(NetworkInterfaceComponent.IPv4);
            Console.WriteLine($"インターフェイス名: {ni.Name}, IPv4対応: {supportsIPv4}");
        }
    }
}
これで、わらわの教えは終わりじゃ。ネットワークの力を存分に活かし、問題を打ち砕くがよい!

0 件のコメント:
コメントを投稿