使用mono在linux中获取MAC地址 [英] Get MAC Address in linux using mono

查看:15
本文介绍了使用mono在linux中获取MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get the MAC address of my computer in a Mono application on Linux?

推荐答案

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

Borrowed from MSDN, tested on VS2008 and 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

这篇关于使用mono在linux中获取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆