■ NetworkInterface 클래스의 GetAllNetworkInterfaces 정적 메소드를 사용해 네트워크 주소 배열을 구하는 방법을 보여준다.
▶ NetworkInterface 클래스 : GetAllNetworkInterfaces 정적 메소드를 사용해 네트워크 주소 배열 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 |
foreach(string networkAddress in GetNetworkAddressArray(NetworkInterfaceType.Ethernet)) { Console.WriteLine(networkAddress); } /* 192.168.29.240 172.18.144.1 172.21.128.1 */ |
▶ NetworkInterface 클래스 : GetAllNetworkInterfaces 정적 메소드를 사용해 네트워크 주소 배열 구하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System.Net.NetworkInformation; using System.Net.Sockets; #region 네트워크 주소 배열 구하기 - GetNetworkAddressArray(networkInterfaceType) /// <summary> /// 네트워크 주소 배열 구하기 /// </summary> /// <param name="networkInterfaceType">네트워크 인터페이스 타입</param> /// <returns>네트워크 주소 배열</returns> public string[] GetNetworkAddressArray(NetworkInterfaceType networkInterfaceType) { List<string> targetList = new List<string>(); foreach(NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { if(networkInterface.NetworkInterfaceType == networkInterfaceType && networkInterface.OperationalStatus == OperationalStatus.Up) { foreach(UnicastIPAddressInformation unicastIPAddressInformation in networkInterface.GetIPProperties().UnicastAddresses) { if(unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) { targetList.Add(unicastIPAddressInformation.Address.ToString()); } } } } return targetList.ToArray(); } #endregion |