111,098
社区成员




/// <summary>
/// 网卡MAC地址。
/// </summary>
static public string MACAddress
{
get
{
if (string.IsNullOrEmpty(_macAddress))
{
string filename = System.IO.Path.Combine(FileUtil.AppDir, "Network.txt");
System.IO.StreamWriter writer = new System.IO.StreamWriter(filename, false);
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
foreach (PropertyData property in mo.Properties)
{
writer.WriteLine("{0} = {1};", property.Name, property.Value);
#if DEBUG
Console.WriteLine("{0} = {1};", property.Name, property.Value);
#endif
}
if ((bool)mo["IPEnabled"] == true)
_macAddress = mo["MacAddress"].ToString();
if (!String.IsNullOrEmpty(_macAddress) && !IsException)
{
writer.Close();
System.IO.File.Delete(filename);
break;
}
mo.Dispose();
}
writer.Close();
moc.Dispose();
}
return _macAddress;
}
}
/// <summary>
/// 获取客户端的mac地址
/// </summary>
/// <param name="IP"></param>
/// <returns></returns>
public string GetCustomerMac(string IP)
{
string dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-a " + IP;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
//匹配mac地址
Match m = Regex.Match(dirResults, "\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w\\w");
//若匹配成功则返回mac,否则返回找不到主机信息
if (m.ToString() != "")
{
return m.ToString();
}
else
{
return "找不到主机信息";
}
}