使用VB.net 2010创建MAC地址

问题描述:

我试图通过在vb.net中使用以下函数来获取每台机器的MAC地址,但我只是意识到这个功能在Windows XP中不起作用,





I have tried to get MAC address of each machine by using the below function in vb.net ,but I just realized that this function doesn't work in windows XP ,,


Function getMacAddress()
       Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
       Return nics(1).GetPhysicalAddress.ToString
   End Function







有人可以帮助我,我该怎么办?也可以在windows xp中解决



谢谢davnce




can someone help me what should I do for solving in windows xp as well

Thanks in davnce

我认为它确实有效XP,我认为问题是你正在访问错误的数组元素:

尝试:

I think it does work in XP, I think the problem is that you are accessing the wrong array element:
Try:
Return nics(0).GetPhysicalAddress.ToString

或者更好的是,循环遍历数组,并返回第一个这是一个以太网适配器并且可以运行。



有这里的提示显示了代码 - 它在C#中,但代码很容易理解,请查看GetMacAddress方法:检索LAN的IP和MAC地址 [ ^ ]

Or better still, loop through the array, and return the first one that is an ethernet adapter and operational.

There is a Tip here that shows the code - it's in C#, but the code is very easy to follow, look at the GetMacAddress method: Retrieving IP and MAC addresses for a LAN[^]


你可能忘记了图书馆



Could be you forgot the library

Imports System.Net.NetworkInformation

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim mac As String
        mac = getMacAddress()
        MessageBox.Show(mac)
    End Sub

    Function getMacAddress()
        Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
        Return nics(1).GetPhysicalAddress.ToString
    End Function

End Class