C#プログラミング_カード情報を検出し、ネットワークトラフィック検出

カード情報の検出、ネットワークトラフィック検出


ハードウェア装置によってすべき各コンピュータケーブル通信を、 ネットワークカード と呼ばれる、 NICは NIC ある Network Interface Controller 略語。NICの責任である 電気信号にビットストリーム 伝送および検出 ビットストリームに電気信号を 受信しました。

ネットワークアダプタ:

  • また、ネットワークに接続されたハードウェア装置は、コンピュータネットワークカードとして知られています。

  • コンピュータ上で適切なサイズのパケットにネットワーク回線に送信されたデータ、及びデータは、ネットワークを整理するために送信された後。

    C#で、それはカードの情報を提供するために、System.Net.NetworkInformation名前空間を提供します。

これらは、次のとおりです。

  • ネットワークカード関連情報の検出
  • このユニットは、NICの数、その名前、速度、ハードウェアアドレスを持っています。
  • マシンは、ネットワークトラフィックを検出し、
    • ネットワーク接続の設定、送信されたデータパケットなどを受信。

カード情報検出関連するクラス

ネットワークインターフェイス类

これは、ネットワークアダプタの設定と統計情報を提供します。

  • ネットワークアダプタの数

  • ネットワークアダプタモデル

  • 高速ネットワークアダプタ

  • ネットワークアダプタのMACアドレス

  • ネットワークアダプタの接続が可能です

NetworkInterface対応するオブジェクトを含む各ネットワークアダプタ。

プロパティとメソッド 説明
Nameプロパティ ネットワークアダプタの名前を取得します。
スピードプロパティ 高速ネットワーク・アダプタ(*ビット/ *秒)を取得します
GetAllNetworkInterfaces方法 オブジェクトの説明は、ローカルコンピュータ上のすべてのネットワークアダプタを返します。
GetIPProperties方法 説明は、このネットワークアダプタの設定のオブジェクトを返します。
Getisnetworkavalble方法 これは、任意の利用可能なネットワーク接続があるかどうかを示します
GetPhysicalAddress方法 アダプタのMAC(Media Access Control)アドレスを返します。
Supportsメソッド インタフェースは、指定されたプロトコル(IPv4またはIPv6)をサポートしているかどうかを示します

情報カードへのアクセス

 private static void NetworkInterfaces()
        {
            #region NetWorkInterface
            //利用NetworkInterface类提供的静态方法得到NetworkInterface类型的数组。
            NetworkInterface[] networkInterface = NetworkInterface.GetAllNetworkInterfaces();//声明并初始化了一个 NetworkInterface类的对象数组。
            Console.WriteLine($"网络适配器的个数为:{networkInterface.Length}");
            Console.WriteLine($"是否有可以用网络连接:{NetworkInterface.GetIsNetworkAvailable()}");
            Console.WriteLine();
            foreach (NetworkInterface network in networkInterface)
            {
                Console.WriteLine($"网卡名字:{network.Name}");
                Console.WriteLine($"物理地址:{network.GetPhysicalAddress()}");
                Console.WriteLine($"速度:{network.Speed}");
                Console.WriteLine($"网卡ID:{network.Id}");
                Console.WriteLine($"网卡描述:{network.Description}");

                Console.WriteLine($"是否仅接受数据包:{network.IsReceiveOnly}");
                Console.WriteLine($"是否支持IPV4:{network.Supports(NetworkInterfaceComponent.IPv4)}");
                Console.WriteLine($"是否支持IPV6:{network.Supports(NetworkInterfaceComponent.IPv6)}");
                Console.WriteLine("-----------------------------------------------------------------");

            }
网卡名字:本地连接* 1
物理地址:144F8A2158EB
速度:-1
网卡ID:{FA9901D2-C3AD-412B-BCC2-D6FF592EE29B}
网卡描述:Microsoft Wi-Fi Direct Virtual Adapter
是否仅接受数据包:False
是否支持IPV4:True
是否支持IPV6:True

IPInterfaceProperties类

  • マシンは、すべてのネットワークアダプタのサポートに様々なアドレスを検出します

    • DNSサーバーのIPアドレス、ゲートウェイアドレス、およびマルチキャストアドレス。
  • IPInterfacePropertiesクラスは抽象的で、インスタンス化することはできません。

    • そのGetIPPropertiesのNetworkInterfaceオブジェクト(によって得られた実施例)

使用IPInterfacePropertiesクラスのプロパティとメソッド

プロパティとメソッド 説明
AnycastAddressesプロパティ 任意のIPブロードキャストアドレスに、このインターフェイスに割り当てられます
DhcpServerAddressesプロパティ 動的ホスト構成プロトコル(DHCP)サーバー、このインターフェイスを取得する住所
DnsAddressesプロパティ このインタフェースアドレス取得ドメインネームシステム(DNS)サーバー
DnsSuffixプロパティ このインターフェイスに関連付けられます、ドメインネームシステム(DNS)サフィックス
GatewayAddressesプロパティ このインタフェースゲートウェイアドレスを取得します。
MulticastAddressesプロパティ このインターフェイスに割り当てられたマルチキャストアドレスを取得
UnicastAddressesプロパティ このインタフェースに割り当てられたユニキャストアドレスを取得します。
GetIPv4Properties方法 このネットワークインターフェイスのインターネットプロトコルバージョンを取得します(IPv4)の構成データ
GetIPv6Properties方法 インターネットプロトコルバージョン(IPv6)の構成データのこのネットワークインターフェイスを取得します

例として、カード情報のユニキャストアドレス

 private static void GetUnicastAdress()
        {
            #region UnicastAddress 网络接口的单播地址
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
                UnicastIPAddressInformationCollection unicasts = iPInterfaceProperties.UnicastAddresses;
                //定义时间格式
                //dddd 周几
                //mmmm 月份
                //yyyy 年份
                //dd   几日
                //hh:mm:ss 时分秒
                // tt  上下午
                string timetype = "dddd, MMMM dd, yyyy  hh: mm: ss tt";//自己定义时间的格式
                if (unicasts.Count > 0)
                {
                    Console.WriteLine(adapter.Description);//打印出网卡的描述
                    foreach (UnicastIPAddressInformation unicastIPAddressInformation in unicasts)
                    {
                        DateTime when;//声明一个当前时间的日期变量

                        Console.WriteLine("单播地址:.................{0}", unicastIPAddressInformation.Address);
                        Console.WriteLine("获取IPv4子网掩码:.........{0}", unicastIPAddressInformation.IPv4Mask);
                        Console.WriteLine("此地址作为首选地址的秒数...{0}", unicastIPAddressInformation.AddressPreferredLifetime);
                        Console.WriteLine("此地址的有效剩余秒数:.....{0}", unicastIPAddressInformation.AddressValidLifetime);
                        Console.WriteLine("DHCP剩余时间:.............{0}", unicastIPAddressInformation.DhcpLeaseLifetime);
                        Console.WriteLine("前缀的长度:...............{0}", unicastIPAddressInformation.PrefixLength);
                        Console.WriteLine("标识前缀的值:.............{0}", unicastIPAddressInformation.PrefixOrigin);
                        Console.WriteLine("标识后缀的值:.............{0}", unicastIPAddressInformation.SuffixOrigin);
                        when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.AddressPreferredLifetime);//计算时间
                        when.ToLocalTime();//转化为本地时间
                        Console.WriteLine("此地址作为首选地址的到期时间为......{0}",
                            when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//当地时间格式
                        when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.AddressValidLifetime);//计算时间
                        when.ToLocalTime();//转化为本地时间
                        Console.WriteLine("此地址的到期时间为..................{0}",
                            when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//当地时间格式

                        when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.DhcpLeaseLifetime);//计算时间
                        when.ToLocalTime();//转化为本地时间
                        Console.WriteLine("DHCP到期时间为.......................{0}",
                            when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//当地时间格式

                    }
                }
            }
            Console.ReadLine();

ゲートウェイアドレスを取得します

 private static void GatewayAddress()
        {
            #region IPInterfaceProperties 
            NetworkInterface[] adapterd = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface networkInterface in adapterd)
            {
                IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties();
                //网关信息
                GatewayIPAddressInformationCollection gatewayIPAddressInformation = iPInterfaceProperties.GatewayAddresses;
                foreach (var item in gatewayIPAddressInformation)
                {
                    Console.WriteLine($"网关地址:..............{gatewayIPAddressInformation[0].Address }");
                    Console.WriteLine();
                }



            }
            Console.ReadLine();

任意のブロードキャストアドレスを取得します

  private static void AnyCastAddress()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var adapter in adapters)
            {
                IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
                IPAddressInformationCollection iPAddressInformation = iPInterfaceProperties.AnycastAddresses;
                if (iPAddressInformation.Count > 0)
                {
                    foreach (var i in iPAddressInformation)
                    {
                        Console.WriteLine($"广播地址为:..........i.Address");
                        Console.WriteLine($"是否在DNS服务器中出现:..........{(i.IsDnsEligible ? "是" : "否")}");//注意此处条件表达式的用法
                        Console.WriteLine($"广播地址为:..........{(i.IsTransient ? "是" : "否")}");
                    }
                }
                else
                {
                    Console.WriteLine("{0}不存在广播地址", adapter.Name);
                }
            }
            Console.ReadLine();
        }

動的ホスト構成プロトコル(DHCP)サーバー、このインターフェイスを取得する住所

private static void DhcpserverIPAddress()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var adapter in adapters)
            {
                IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
                IPAddressCollection iPAddressInformation = iPInterfaceProperties.DhcpServerAddresses;
                if (iPAddressInformation.Count > 0)
                {
                    foreach (var i in iPAddressInformation)
                    {
                        Console.WriteLine($"广播地址为:..........{i.Address}");

                    }
                    Console.ReadLine();
                }

            }
        }

このインタフェースアドレス取得ドメインネームシステム(DNS)サーバー

 private static void DnsSeverAddress()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var adapter in adapters)
            {
                IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
                IPAddressCollection dnsServers = iPInterfaceProperties.DnsAddresses;
                if (dnsServers.Count > 0)
                {
                    Console.WriteLine(adapter.Description);
                    foreach (var i in dnsServers)
                    {
                        Console.WriteLine($"dns服务器地址为:..........{i.ToString()}");

                    }

                }

            }
            Console.ReadLine();
        }

このネットワークインターフェイスのインターネットプロトコルバージョンを取得します(IPv4)の構成データ

 public static void DisplayIPv4NetworkInterfaces()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            Console.WriteLine("IPv4 interface information for {0}.{1}",
               properties.HostName, properties.DomainName);//获取计算机名字和域
            Console.WriteLine();

            foreach (NetworkInterface adapter in nics)
            {
                // Only display informatin for interfaces that support IPv4.
                if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)//判断是否支持IPV4
                {
                    continue;//如果不支持,直接跳出循环
                }
                Console.WriteLine(adapter.Description);//打印出网卡的描述
                // Underline the description.
                Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                // Try to get the IPv4 interface properties.
                IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();

                if (p == null)
                {
                    Console.WriteLine("No IPv4 information is available for this interface.");
                    Console.WriteLine();
                    continue;
                }
                // Display the IPv4 specific data.
                Console.WriteLine("  Index ............................. : {0}", p.Index);
                Console.WriteLine("  MTU ............................... : {0}", p.Mtu);
                Console.WriteLine("  APIPA active....................... : {0}",
                    p.IsAutomaticPrivateAddressingActive);
                Console.WriteLine("  APIPA enabled...................... : {0}",
                    p.IsAutomaticPrivateAddressingEnabled);
                Console.WriteLine("  Forwarding enabled................. : {0}",
                    p.IsForwardingEnabled);
                Console.WriteLine("  Uses WINS ......................... : {0}",
                    p.UsesWins);
                Console.WriteLine();
            }
        }

のヒントアライメント

 private static void printFenge()
        {
            string a = "1234567890";
            Console.WriteLine(a);
            Console.WriteLine(String.Empty.PadLeft(a.Length, '='));
            Console.WriteLine(String.Empty.PadLeft(a.Length, '*'));
            Console.WriteLine(String.Empty.PadRight(a.Length, 'n'));
        }

概要

デバッガを学ぶために

ブレークポイントデバッグすることで問題を解決します。

公式ドキュメントをチェックすることを学びます

非常に明確に書かれた公式

リリース7件のオリジナルの記事 ウォンの賞賛0 ビュー389

おすすめ

転載: blog.csdn.net/c1ata/article/details/104692920