如何使用C#.net获取mac地址 [英] how to get mac address using C#.net

查看:72
本文介绍了如何使用C#.net获取mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问



I have a question concerning the code below

using System;
using System.Text;
using System.Windows;
using System.Net.NetworkInformation;

namespace get_nick_mac
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //for each j you can get the MAC
            PhysicalAddress address = nics[j].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("-");
                }
            }
        }
    }
}





我得到这个例外

当前上下文中不存在名称j

在线物理地址= nics [j] .GetPhysicalAddress();



感谢先进的



I get this exception
The name "j" does not exist in the current context
On line PhysicalAddress address = nics[j].GetPhysicalAddress();

Thanks in advanced

推荐答案

本文解释了3种获取Mac地址的方法 -



http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/ [ ^ ]



并使用此链接Too-



http://www.dotneter.com/get-mac-address-using- ç [< a href =http://www.dotneter.com/get-mac-address-using-ctarget =_ blanktitle =New Window> ^ ]



代码项目这个问题被问到b4-



使用c#获取消息框中的MAC地址 [ ^ ]
This Article Explained 3 Ways to Get Mac Address-

http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/[^]

and use This Link Too-

http://www.dotneter.com/get-mac-address-using-c[^]

On Code Project this Question Asked b4-

get MAC address in message box using c#[^]


它应该可以工作,也许:

it should work, perhaps :
public static PhysicalAddress GetMacAddress()
            {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                    nic.OperationalStatus == OperationalStatus.Up)
                    {
                    return nic.GetPhysicalAddress();
                    }
                }
            return null;
            }


这篇关于如何使用C#.net获取mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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