Created
June 28, 2018 11:22
-
-
Save scriptam/61b2a0e7d471a2beabfecba031d6a0f5 to your computer and use it in GitHub Desktop.
Returns objects that describe the network interfaces on the device.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.NetworkInformation; | |
namespace BoynerTest | |
{ | |
/// <summary> | |
/// Cross-platform API for network information. | |
/// </summary> | |
public static class NetworkInfo | |
{ | |
/// <summary> | |
/// Get all network interfaces on the device. Network interfaces provide network connectivity, they are also known as network adapters. | |
/// </summary> | |
/// <returns> | |
/// A NetworkInterface array that contains objects that describe the available network interfaces, or an empty array if no interfaces are detected. | |
/// </returns> | |
public static NetworkInterface[] GetInterfaces() | |
{ | |
return NetworkInterface.GetAllNetworkInterfaces(); | |
} | |
public static string GetMacAddress() | |
{ | |
var interfaces = GetInterfaces(); | |
foreach (var netInterface in interfaces) | |
{ | |
if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || | |
netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet) | |
{ | |
var address = netInterface.GetPhysicalAddress(); | |
return BitConverter.ToString(address.GetAddressBytes()); | |
} | |
} | |
return "NoMac"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment