Skip to content

Instantly share code, notes, and snippets.

@scriptam
Created June 28, 2018 11:22
Show Gist options
  • Save scriptam/61b2a0e7d471a2beabfecba031d6a0f5 to your computer and use it in GitHub Desktop.
Save scriptam/61b2a0e7d471a2beabfecba031d6a0f5 to your computer and use it in GitHub Desktop.
Returns objects that describe the network interfaces on the device.
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