c# - 使用单声道在 Linux 中获取 MAC 地址

标签 c# linux mono

如何在 Linux 上的 Mono 应用程序中获取计算机的 MAC 地址?

最佳答案

借自MSDN , 在 VS2008 和 mono 2.4.2.3 (Debian 2.4.2.3+dfsg-2) 上测试:

using System;
using System.Net.NetworkInformation;

namespace ConsoleApplication2
{
    class Program
    {
        public static void ShowNetworkInterfaces()
        {
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            Console.WriteLine("Interface information for {0}.{1}     ",
                    computerProperties.HostName, computerProperties.DomainName);

            if (nics == null || nics.Length < 1)
            {
                Console.WriteLine("  No network interfaces found.");
                return;
            }

            Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);

            foreach (NetworkInterface adapter in nics)
            {
                Console.WriteLine();
                Console.WriteLine(adapter.Description);
                Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
                Console.WriteLine("  Interface type .......................... : {0}", adapter.Netwo$
                Console.Write("  Physical address ........................ : ");
                PhysicalAddress address = adapter.GetPhysicalAddress();
                byte[] bytes = address.GetAddressBytes();
                for (int i = 0; i < bytes.Length; i++)
                {
                    // Display the physical address in hexadecimal.
                    Console.Write("{0}", bytes[i].ToString("X2"));
                    // Insert a hyphen after each byte, unless we are at the end of the
                    // address.
                    if (i != bytes.Length - 1)
                    {
                        Console.Write("-");
                    }
                }
                Console.WriteLine();
            }
        }

        static void Main(string[] args)
        {
            ShowNetworkInterfaces();
        }
    }
}

输出(Linux):

Interface information for hera.(none)
  Number of interfaces .................... : 2

lo
==
  Interface type .......................... : Loopback
  Physical address ........................ :

eth0
====
  Interface type .......................... : Ethernet
  Physical address ........................ : 00-26-xx-xx-xx-xx

关于c# - 使用单声道在 Linux 中获取 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1746840/

相关文章:

f# - VSCode FSharp Interactive 终端中的奇怪字符

mono - 与 OS X 上的 Mono 相比,Windows 上 .NET 3.5 的 TimeZoneInfo 实例有区别吗?

c# - Autofac 多次注册组件

c# - 我们如何使用 Visual Studio 2008 发布一个 asp.net 网站?

python - 创建命令 Python 的输出

Linux:在 docker 容器中找不到现有的共享库

c# - 我可以将哪些命令行构建工具(如 make)与 Mono C# 一起使用?

c# - 从数据库加载类的最佳方式 (C#)

用于 Linux 和/或 MonoDevelop 的 C# 代码格式化程序

php - CentOS 不支持阿拉伯语