using System; using System.Net.NetworkInformation; using System.Net.Sockets; namespace ZeroLevel.Services.Network.Utils.Networks { /// /// Wired Network. /// public static class Wired { /// /// Gets the interface name. /// /// The interface name. public static string Name { get { try { foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) { return item.Name; } } return ""; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return ""; } } } /// /// Gets a value indicating if wired network is up. /// /// true if network is up; otherwise, false. public static bool IsUp { get { try { foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) { return true; } } return false; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return false; } } } /// /// Gets the IP address. /// /// The IP address. public static string IPAddress { get { try { string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } } } return output; } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); return ""; } } } } }