有的时候需要根据MAC来限定登录的计算机,为此查找了不少资料(有来自博客堂和CSDN),下面是获得远程计算机的MAC和局域网中计算机列表的方法。
需要引用的命名空间
using System;
using System.Collections;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
获得本机的MAC地址
public static string GetLocalMac()
{
string strMac = string.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
strMac += mo["MacAddress"].ToString() ;
}
return strMac.ToUpper();
}
获得远程计算机的MAC地址
方法一:使用API,利用ARP协议,只能获得同网段计算机的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);
public static string GetRemoteMac(string clientIP)
{
string ip = clientIP;
if ( ip == "127.0.0.1")
ip = GetLocalIP()[0];
Int32 ldest=inet_addr(ip);
Int64 macinfo=new Int64();
Int32 len=6;
try
{
SendARP(ldest,0,ref macinfo,ref len);
}
catch
{
return "";
}
string originalMACAddress = Convert.ToString(macinfo,16);
if (originalMACAddress.Length<12)
{
originalMACAddress = originalMACAddress.PadLeft(12,'0');
}
string macAddress;
if(originalMACAddress!="0000" && originalMACAddress.Length==12)
{
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="";
}
return macAddress.ToUpper();
}
方法二:使用windows的命令nbtstat
public static string GetRemoteMacByNetBIOS(string clientIP)
{
string ip = clientIP;
if ( ip == "127.0.0.1")
ip = GetLocalIP()[0];
string dirResults="";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat.exe";
//psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;psi.RedirectStandardError=true;
psi.Arguments = "-A " + ip;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t","");
Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match mc=reg.Match(dirResults+"__MAC");
if(mc.Success)
{
return mc.Groups["key"].Value.ToUpper();
}
else
{
return "";
}
}
使用此方法需要足够的操作系统的权限。在Web中,可以将ASP.net用户加入管理员组。
对于上面两个地方都用到的GetLocalIP是一个获取本机IP的方法:
public static string[] GetLocalIP()
{
string hostName = Dns.GetHostName();
IPHostEntry ipEntry=Dns.GetHostByName(hostName);
IPAddress[] arr=ipEntry.AddressList;
string[] result = new string[arr.Length];
for(int i=0;i
{
result[i] = arr[i].ToString();
}
return result;
}