如何在c#和vb.net中获取客户端机器的MAC地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2018982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get MAC address of client machine in c# and vb.net
提问by Azhar
How to get MAC address of client machine in c# and vb.net
如何在c#和vb.net中获取客户端机器的MAC地址
采纳答案by Darin Dimitrov
I am not sure what you mean by client machine, because you can only get the MAC address of a NIC of the machine your application executes under.
我不确定您所说的client machine是什么意思,因为您只能获取应用程序在其下执行的机器的 NIC 的 MAC 地址。
For this you could use ManagementClass:
为此,您可以使用ManagementClass:
C#:
C#:
using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
foreach(ManagementObject mo in mc.GetInstances())
{
Console.WriteLine(mo["MacAddress"].ToString());
}
}
VB.NET:
VB.NET:
Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
For Each mo As ManagementObject In mc.GetInstances()
Console.WriteLine(mo("MacAddress").ToString())
Next
End Using
回答by braindice
This should work in vb - i am sure c# is close to this
Import the following namespace.
这应该在 vb 中工作 - 我确信 c# 接近这个
导入以下命名空间。
Imports System.Management
Declare following object variables.
声明以下对象变量。
Dim objMOS As ManagementObjectSearcher
Dim objMOC As Management.ManagementObjectCollection
Dim objMO As Management.ManagementObject
Execute the query.
执行查询。
objMOS = New ManagementObjectSearcher("Select * From Win32_NetworkAdapter")
objMOC = objMOS.Get
Get MAC address from the query result.
For Each objMO In objMOC
MessageBox.Show(objMO("MACAddress"))
Next
Dispose object variables.
处理对象变量。
objMOS.Dispose()
objMOS = Nothing
objMO.Dispose()
objMO = Nothing
回答by Azhar
the desired answer is
想要的答案是
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["ipEnabled"])
continue;
Console.WriteLine((string)objMO["MACAddress"]);
}