会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
在云那方
首页
新随笔
联系
订阅
管理
公告
C#取网卡Mac地址
using
System;
using
System.Runtime.InteropServices;
using
System.Text;
using
System.Text.RegularExpressions;
namespace
TelEvent
{
/**/
///
///
HardwareInfo 的摘要说明。
///
public
class
HardwareInfo
{
[DllImport(
"
Iphlpapi.dll
"
)]
private
static
extern
int
SendARP(Int32 dest,Int32 host,
ref
Int32 mac,
ref
Int32 length);
[DllImport(
"
Ws2_32.dll
"
)]
private
static
extern
Int32 inet_addr(
string
ip);
public
static
string
GetMACFromIP(
string
A_strIP)
{
string
strRet
=
"
00-00-00-00-00-00
"
;
string
strIPPattern
=
@"
^\d+\.\d+\.\d+\.\d+$
"
;
Regex objRex
=
new
Regex(strIPPattern);
if
(objRex.IsMatch(A_strIP)
==
true
)
{
Int32 intDest
=
inet_addr(A_strIP);
Int32 [] arrMAC
=
new
Int32[
2
];
Int32 intLen
=
6
;
int
intResult
=
SendARP(intDest,
0
,
ref
arrMAC[
0
],
ref
intLen);
if
(intResult
==
0
)
{
Byte [] arrbyte
=
new
Byte[
8
];
arrbyte[
5
]
=
(Byte)(arrMAC[
1
]
>>
8
);
arrbyte[
4
]
=
(Byte)arrMAC[
1
];
arrbyte[
3
]
=
(Byte)(arrMAC[
0
]
>>
24
);
arrbyte[
2
]
=
(Byte)(arrMAC[
0
]
>>
16
);
arrbyte[
1
]
=
(Byte)(arrMAC[
0
]
>>
8
);
arrbyte[
0
]
=
(Byte)arrMAC[
0
];
StringBuilder strbMAC
=
new
StringBuilder();
for
(
int
intIndex
=
0
;intIndex
<
6
;intIndex
++
)
{
if
(intIndex
>
0
) strbMAC.Append(
"
-
"
);
strbMAC.Append(arrbyte[intIndex].ToString(
"
X2
"
));
}
strRet
=
strbMAC.ToString();
}
}
return
strRet;
}
}
}
posted on
2008-07-04 12:45
Rich.T
阅读(
501
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 Rich.T
Powered by .NET 8.0 on Kubernetes