zoukankan
html css js c++ java
C# 获取 MAC地址!
最近项目中需要一个方法,来获取本机的MAC地址。
本人最后找到两种方法来实现此功能:
方法一:直接获取
using
System;
using
System.Management;
namespace
PublicBill.GetMAC
{
class
GetMAC
{
[STAThread]
static
void
Main(
string
[] args)
{
string
mac
=
""
;
ManagementClass mc
=
new
ManagementClass(
"
Win32_NetworkAdapterConfiguration
"
);
ManagementObjectCollection moc
=
mc.GetInstances();
foreach
(ManagementObject mo
in
moc)
{
if
(mo[
"
IPEnabled
"
].ToString()
==
"
True
"
)
{
mac
=
mo[
"
MacAddress
"
].ToString();
}
}
Console.WriteLine(
"
MAC Address:
"
+
mac.ToString());
}
}
}
方法二:ARP协议的解析原理获取MAC地址
using
System;
using
System.Runtime.InteropServices;
using
System.Net;
namespace
GetMacAddress
{
class
MAC
{
[DllImport(
"
Iphlpapi.dll
"
)]
private
static
extern
int
SendARP(Int32 dest,Int32 host,
ref
ulong
mac,
ref
IntPtr length);
[DllImport(
"
Ws2_32.dll
"
)]
private
static
extern
Int32 inet_addr(
string
ip);
[STAThread]
static
void
Main(
string
[] args)
{
IPHostEntry hostInfo
=
Dns.GetHostByName(Dns.GetHostName());
IPAddress[] addrs
=
hostInfo.AddressList;
IPAddress ipAddress
=
addrs[
0
];
Int32 ldest
=
inet_addr(ipAddress.ToString());
try
{
Int32 length
=
6
;
ulong
mac
=
0
;
IntPtr len
=
new
IntPtr(length);
int
ii
=
SendARP(ldest,
0
,
ref
mac,
ref
len);
string
l
=
""
;
string
h
=
""
;
int
n
=
1
;
long
m
=
0
;
long
lm
=
(
long
)mac;
while
(lm
>
0
)
{
m
=
lm
%
16
;
lm
=
lm
/
16
;
if
(m
>
9
)
{
if
(m
==
10
)l
=
"
A
"
+
l;
if
(m
==
11
)l
=
"
B
"
+
l;
if
(m
==
12
)l
=
"
C
"
+
l;
if
(m
==
13
)l
=
"
D
"
+
l;
if
(m
==
14
)l
=
"
E
"
+
l;
if
(m
==
15
)l
=
"
F
"
+
l;
}
else
{
l
=
m.ToString()
+
1
;
}
if
((n
%
2
)
==
0
)
{
h
=
h
+
1
;
l
=
""
;
}
++
n;
}
Console.WriteLine(
"
IP Address:
"
+
ipAddress.ToString());
Console.WriteLine(
"
MAC Address:
"
+
h);
}
catch
(Exception error)
{
Console.WriteLine(error);
}
}
}
}
查看全文
相关阅读:
金蝶K3 账套复制
silverlight控件如何自适应
一个使用Jquery写的一个鼠标拖动效果
如何使用C#开发“类ActiveX组件”
一个非科班出身程序员的成长历程
Ubuntu 12.04下LAMP安装配置
Abp vue项目找不到模块“./app.vue”
google的分析(analytics)js代码分析以及重写
javascript实现类似google和msn space的拖拽
Lucene.Net, SQL Server 2008全文检索, Like模糊查询的一点心得
原文地址:https://www.cnblogs.com/publicbill/p/250766.html
最新文章
贵人
JAVA分布式事务原理及应用(转)
一些高效的Linux命令行操作 (转自君淋天下的博客)
win7 DB2 控制中心乱码(转)
java I/O总结(收藏)
Xamarin.Forms 复制本地SQLite数据库
生成等长随机数值的方法
C# Winform 自定义异常处理方法
php函数内不能访问函数外的变量原因
分组取前N记录 一道淘宝的考察sql语句的面试题
热门文章
PHP中获取文件扩展名的N种方法
大固其其
MySQL中的BLOB类型
mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
让新生更了解我们
PHP为什么会被认为是草根语言?
极限编程在WEB开发中的作用
Ubuntu20.0下安装eclipse
金蝶k3系统如何设置业务单据打印模板
金蝶K3 MRP 简易操作
Copyright © 2011-2022 走看看