Last active
December 15, 2015 23:09
-
-
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#
This file contains 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; | |
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 ); | |
} | |
} | |
} | |
} |
This file contains 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
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