如何读取本机的网卡号?
解决方案 »
- c# combobox设置dropdownlist后如何设置默认值?
- C# ActiveX控件在IE上面运行慢
- 帮忙注释下这个方法,最好每一行都要注释
- 如何让组合框combobox,初始化时让某一项成为当前项呢?
- c# 结构中含有结构和结构数组到 byte[] 变量的转换操作?
- 关于Webbrowser的显示问题
- 谁能给我一个多线程+Access+处理窗口中进度描述的范例?!!!
- 子类用 override 重写和 new 隐藏父类的方法,有何不同?
- 我使用.NET的Dom对象生成的xml文件,如何保证失UTF-8编码的.
- 有关 ArrayList 的小问题,会的进来看看啊!
- 如何制作具有xp风格的菜单
- SQL Server不存在或拒绝被访问
* 获取MAC地址
* */
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
static private Int64 getRemoteMAC(string localIP, string remoteIP)
{
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
} private string getMacString(Int64 macinfo)
{
string macAddress = "";
string originalMACAddress=macinfo.ToString("X4");
if(originalMACAddress!="0000" && originalMACAddress.Length==12)
{ //合法MAC地址
string mac1,mac2,mac3,mac4,mac5,mac6;
mac1=originalMACAddress.Substring(10,2);
mac2=originalMACAddress.Substring(8,2);
mac3=originalMACAddress.Substring(6,2);
mac4=originalMACAddress.Substring(4,2);
mac5=originalMACAddress.Substring(2,2);
mac6=originalMACAddress.Substring(0,2);
macAddress=mac1+"-"+mac2+"-"+mac3+"-"+mac4+"-"+mac5+"-"+mac6;
}
else
{
macAddress="00-00-00-00-00-00";
}
return macAddress;
}
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());
}
}
}