微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用单声道获取linux中的MAC地址

如何在 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

原文地址:https://www.jb51.cc/csharp/91915.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐