Reliable method to get machine's MAC address in C#

Reliable method to get machine's MAC address in C#

In C#, you can retrieve the MAC address of a machine using the NetworkInterface class from the System.Net.NetworkInformation namespace. Here's a simple example:

using System; using System.Net.NetworkInformation; class Program { static void Main(string[] args) { // Get all network interfaces on the machine NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface networkInterface in networkInterfaces) { // Filter out loopback and other non-physical interfaces if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback && networkInterface.OperationalStatus == OperationalStatus.Up) { // Get the MAC address PhysicalAddress macAddress = networkInterface.GetPhysicalAddress(); // Convert MAC address to string string macAddressString = BitConverter.ToString(macAddress.GetAddressBytes()); // Print MAC address Console.WriteLine($"MAC Address: {macAddressString}"); } } } } 

This code iterates through all network interfaces, filters out loopback and non-operational interfaces, and then retrieves the MAC address for each active interface. It then prints out the MAC address in string format.

Examples

  1. Search Query: How to get the MAC address of the first network adapter in C#?

    • Description: This method retrieves the MAC address of the first network adapter found on the machine.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; class Program { static void Main() { var firstAdapter = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(); if (firstAdapter != null) { Console.WriteLine(firstAdapter.GetPhysicalAddress().ToString()); } } } 
  2. Search Query: How to get all MAC addresses of network adapters in C#?

    • Description: This method retrieves MAC addresses of all network adapters on the machine.
    • Code:
      using System; using System.Net.NetworkInformation; class Program { static void Main() { var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (var adapter in networkInterfaces) { Console.WriteLine(adapter.GetPhysicalAddress().ToString()); } } } 
  3. Search Query: How to get the MAC address of a specific network adapter by name in C#?

    • Description: This method retrieves the MAC address of a network adapter by its name.
    • Code:
      using System; using System.Net.NetworkInformation; class Program { static void Main() { string adapterName = "Ethernet"; var adapter = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault(n => n.Name == adapterName); if (adapter != null) { Console.WriteLine(adapter.GetPhysicalAddress().ToString()); } } } 
  4. Search Query: How to get the MAC address of an active network adapter in C#?

    • Description: This method retrieves the MAC address of an active network adapter that is currently up.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; class Program { static void Main() { var activeAdapter = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault(n => n.OperationalStatus == OperationalStatus.Up); if (activeAdapter != null) { Console.WriteLine(activeAdapter.GetPhysicalAddress().ToString()); } } } 
  5. Search Query: How to get the MAC address in a console application in C#?

    • Description: This method demonstrates how to retrieve and print the MAC address in a console application.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; class Program { static void Main() { var macAddress = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault()? .GetPhysicalAddress() .ToString(); Console.WriteLine(macAddress); } } 
  6. Search Query: How to get the MAC address in a Windows Forms application in C#?

    • Description: This method demonstrates how to retrieve and display the MAC address in a Windows Forms application.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; using System.Windows.Forms; public class MainForm : Form { public MainForm() { var label = new Label { Dock = DockStyle.Fill, TextAlign = System.Drawing.ContentAlignment.MiddleCenter }; var macAddress = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault()? .GetPhysicalAddress() .ToString(); label.Text = macAddress; this.Controls.Add(label); } [STAThread] public static void Main() { Application.Run(new MainForm()); } } 
  7. Search Query: How to get the MAC address of a network adapter by interface type in C#?

    • Description: This method retrieves the MAC address of a network adapter by its network interface type (e.g., Ethernet, Wireless).
    • Code:
      using System; using System.Net.NetworkInformation; class Program { static void Main() { var adapter = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault(n => n.NetworkInterfaceType == NetworkInterfaceType.Ethernet); if (adapter != null) { Console.WriteLine(adapter.GetPhysicalAddress().ToString()); } } } 
  8. Search Query: How to handle exceptions when getting MAC addresses in C#?

    • Description: This method includes exception handling when retrieving MAC addresses to ensure the program handles errors gracefully.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; class Program { static void Main() { try { var macAddress = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault()? .GetPhysicalAddress() .ToString(); Console.WriteLine(macAddress); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } 
  9. Search Query: How to convert MAC address to a human-readable format in C#?

    • Description: This method formats the MAC address as a human-readable string with colons separating each byte.
    • Code:
      using System; using System.Linq; using System.Net.NetworkInformation; using System.Text; class Program { static void Main() { var macAddress = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault()? .GetPhysicalAddress() .GetAddressBytes(); if (macAddress != null) { var macString = BitConverter.ToString(macAddress).Replace("-", ":"); Console.WriteLine(macString); } } } 
  10. Search Query: How to get the MAC address using WMI in C#?

    • Description: This method retrieves the MAC address using Windows Management Instrumentation (WMI), which can provide more detailed information.
    • Code:
      using System; using System.Management; class Program { static void Main() { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"); foreach (ManagementObject obj in searcher.Get()) { Console.WriteLine(obj["MACAddress"].ToString()); } } } 

More Tags

pandas-datareader azure-log-analytics casing uitextviewdelegate subsequence uipickerview android-textureview android-filterable firebase-tools visual-studio-code

More Programming Questions

More Housing Building Calculators

More Chemistry Calculators

More Retirement Calculators

More Entertainment Anecdotes Calculators