最强版C#获取机器IP MAC信息
作者:Lynn 日期:2009-08-31
/*最强版C#获取机器IP MAC信息*/string GetMacAddress()
{
string mac = "";
try
{
//获取网卡硬件地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = " -A " + GetIP();
p.Start();
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
if (Regex.IsMatch(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase))
{
mac = Regex.Match(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase).Groups[1].Value;
}
//mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
return mac;
}
catch
{
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ipconfig";
p.StartInfo.Arguments = "/all";
p.Start();
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
if (Regex.IsMatch(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase))
{
mac = Regex.Match(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase).Groups[1].Value;
}
//mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
return mac;
}
catch
{
return "unknow";
}
}
//return "unknow";
}
finally
{
}
}
public static string GetIP()
{
string s = "";
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
s += addressList[i].ToString();
}
return s;
}
{
string mac = "";
try
{
//获取网卡硬件地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = " -A " + GetIP();
p.Start();
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
if (Regex.IsMatch(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase))
{
mac = Regex.Match(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase).Groups[1].Value;
}
//mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
return mac;
}
catch
{
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ipconfig";
p.StartInfo.Arguments = "/all";
p.Start();
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
if (Regex.IsMatch(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase))
{
mac = Regex.Match(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase).Groups[1].Value;
}
//mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
return mac;
}
catch
{
return "unknow";
}
}
//return "unknow";
}
finally
{
}
}
public static string GetIP()
{
string s = "";
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
s += addressList[i].ToString();
}
return s;
}
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags: