62,046
社区成员
发帖
与我相关
我的任务
分享
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
GetIPAddress();
}
public static string ReadMac(string ip)//传递IP地址,即可返回MAC地址
{
string mac = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = "-a " + ip;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
int len = output.IndexOf("MAC Address = ");
if(len>0)
{
mac = output.Substring(len + 14, 17);
}
p.WaitForExit();
return mac;
}
public void GetIPAddress()
{
DirectoryEntry entryPC = new DirectoryEntry("WinNT:");
ArrayList arr=new ArrayList();
foreach(DirectoryEntry child in entryPC.Children)
{
TreeNode node = new TreeNode();
node.Text = child.SchemaClassName+":"+child.Name;
PCTree.Nodes.Add(node);
foreach(DirectoryEntry pc in child.Children)
{
if(String.Compare(pc.SchemaClassName,"computer",true)==0)
{
TreeNode son = new TreeNode();
//son.Text = pc.Name;
try
{
IPHostEntry hostent = Dns.GetHostByName(pc.Name); // 主机信息
Array addrs = hostent.AddressList; // IP地址数组
IEnumerator it = addrs.GetEnumerator(); // 迭代器
while(it.MoveNext())
{ // 循环到下一个IP 地址
IPAddress ip = (IPAddress)it.Current; // 获得 IP 地址
son.Text=ip.ToString(); // 显示 IP地址
arr.Add(ReadMac(ip.ToString()));
}
}
catch
{
son.Text=pc.Name;
}
node.Nodes.Add(son);
}
}
}
foreach(string s in arr)
{
this.listBox1.Items.Add(s);
}
}