Skip to content

Instantly share code, notes, and snippets.

@serge1
Last active December 15, 2015 23:09
Show Gist options
  • Save serge1/5337896 to your computer and use it in GitHub Desktop.
Save serge1/5337896 to your computer and use it in GitHub Desktop.
Retrieve and output MAC addresses for all network interfaces of local machine in C#
using System;
using System.Net.NetworkInformation;
using System.Linq;
namespace Test {
// Retrieve and output MAC addresses for all network interfaces of local machine
public class Program {
static void Main()
{
var macAddrs =
from nic in NetworkInterface.GetAllNetworkInterfaces()
select nic.GetPhysicalAddress().ToString();
Console.WriteLine( "Total number of interfaces: {0}",
macAddrs.Count() );
int i = 0;
foreach( var macAddr in macAddrs ) {
Console.WriteLine( "{0} = {1}", ++i, macAddr );
}
}
}
// Retrieve and output MAC addresses for active network interfaces of local machine
class Program1 {
static void Main()
{
var macAddrs =
from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString();
Console.WriteLine( "Total number of active interfaces: {0}",
macAddrs.Count() );
int i = 0;
foreach( var macAddr in macAddrs ) {
Console.WriteLine( "{0} = {1}", ++i, macAddr );
}
}
}
}
Compile with the following command line:
csc /m:Test.Program test.cs
or
csc /m:Test.Program1 test.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment