개발/C# .NET

C#에서 네트웍정보 조회하기

xwing 2008. 8. 27. 21:05
System.Net.NetworkInformation를 이용해서 내컴퓨터의 네트웍정보표시하기


    1 using System;

    2 using System.Net;

    3 using System.Net.NetworkInformation;

    4 

    5 namespace NetworkCard

    6 {

    7     public class NetworkInterfaces

    8     {

    9 

   10         public static void Main(string[] args)

   11         {

   12             IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();

   13             NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

   14             Console.WriteLine("Interface information for {0}.{1}    ", computerProperties.HostName, computerProperties.DomainName);

   15             if (nics == null || nics.Length < 1)

   16             {

   17                 Console.WriteLine("  No network interfaces found.");

   18                 return;

   19             }

   20 

   21             Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);

   22             foreach (NetworkInterface adapter in nics)

   23             {

   24                 IPInterfaceProperties properties = adapter.GetIPProperties();

   25                 Console.WriteLine();

   26                 Console.WriteLine(adapter.Description);

   27                 Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));

   28                 Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);

   29                 Console.WriteLine("  Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString());

   30                 Console.WriteLine("  Operational status ...................... : {0}", adapter.OperationalStatus);

   31                 string versions = "";

   32 

   33                 // Create a display string for the supported IP versions.

   34                 if (adapter.Supports(NetworkInterfaceComponent.IPv4))

   35                 {

   36                     versions = "IPv4";

   37                 }

   38                 if (adapter.Supports(NetworkInterfaceComponent.IPv6))

   39                 {

   40                     if (versions.Length > 0)

   41                     {

   42                         versions += " ";

   43                     }

   44                     versions += "IPv6";

   45                 }

   46                 Console.WriteLine("  IP version .............................. : {0}", versions);

   47 

   48                 // The following information is not useful for loopback adapters.

   49                 if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)

   50                 {

   51                     continue;

   52                 }

   53                 Console.WriteLine("  DNS suffix .............................. : {0}", properties.DnsSuffix);

   54 

   55                 string label;

   56                 if (adapter.Supports(NetworkInterfaceComponent.IPv4))

   57                 {

   58                     IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();

   59                     Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);

   60                     if (ipv4.UsesWins)

   61                     {

   62 

   63                         IPAddressCollection winsServers = properties.WinsServersAddresses;

   64                         if (winsServers.Count > 0)

   65                         {

   66                             label = "  WINS Servers ............................ :";

   67                         }

   68                     }

   69                 }

   70 

   71                 Console.WriteLine("  DNS enabled ............................. : {0}", properties.IsDnsEnabled);

   72                 Console.WriteLine("  Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled);

   73                 Console.WriteLine("  Receive Only ............................ : {0}", adapter.IsReceiveOnly);

   74                 Console.WriteLine("  Multicast ............................... : {0}", adapter.SupportsMulticast);

   75 

   76                 Console.WriteLine();

   77             }

   78         }

   79 

   80     }

   81 }

참고 : http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx

'개발 > C# .NET' 카테고리의 다른 글

web query분석기  (0) 2008.08.27
C#에서 Oracle 연결하기  (0) 2008.05.16
Echo Server  (0) 2008.05.13