搜索局域网内机算机(机器名、IP。MAC地址)

本文介绍了一个使用 C# 实现的简单应用程序,该程序能够查询计算机所在域内的所有计算机,并获取它们的名称、IP 地址及 MAC 地址。通过调用 Win32 API 函数 SendARP 和 inet_addr 来实现 ARP 请求及 IP 地址转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Runtime.InteropServices;
using  System.Net;
using  System.Net.Sockets;
using  System.DirectoryServices;



namespace  ipcheck
{
         
//private System.ComponentModel.Container components = null;
    public partial class MainForm : Form
    
{
        
public MainForm()
        
{
            InitializeComponent();
        }

        [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);
        
private string     macAddress,ipAddress;

        
private bool canPing = false;
        
private string  GetMacAddress() // 得到指定IP的MAC地址 
        {
            Int32 ldest 
= 0;
            
try
            
{
                ldest 
= inet_addr(ipAddress);
            }

            
catch (Exception iperr)
            
{
                MessageBox.Show(iperr.Message);
            }

            Int64 macinfo 
= new Int64();
            Int32 len 
= 6;
            
try
            
{
                
int res = SendARP(ldest, 0ref macinfo, ref len);
            }

            
catch (Exception err)
            
{
                
//    throw new Exception("在解析MAC地址过程发生了错误!"); 
                MessageBox.Show(err.Message);
            }

            
string originalMACAddress = macinfo.ToString("X4");
            
if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
            
//合法MAC地址 
                string mac1, mac2, mac3, mac4, mac5, mac6;
                mac1 
= originalMACAddress.Substring(102);
                mac2 
= originalMACAddress.Substring(82);
                mac3 
= originalMACAddress.Substring(62);
                mac4 
= originalMACAddress.Substring(42);
                mac5 
= originalMACAddress.Substring(22);
                mac6 
= originalMACAddress.Substring(02);
                macAddress 
= mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
                canPing 
= true;
            }

            
else
            
{
                macAddress 
= "无法探测到MAC地址";
                canPing 
= false;
            }

            
return macAddress;
        }

        
private void tbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        
{
            
switch (e.Button.Name)
            

                
case "Btn_search":
                    
int i = 0;
                    DirectoryEntry root 
= new DirectoryEntry("WinNT:");
                    DirectoryEntries domains 
= root.Children;
                    domains.SchemaFilter.Add(
"domain");
                         
foreach (DirectoryEntry  domain in domains)
                         
{
                             
this.g_list.Items.Add(domain.Name.ToString());
                             
                             DirectoryEntries computers 
= domain.Children;
                             computers.SchemaFilter.Add(
"computer");
                                
foreach (DirectoryEntry computer in computers)
                             
{
                                 
this.Grid_view.Rows.Add();
                                 
this.Grid_view.Rows[i].Cells[0].Value = i + 1;
                                 
this.Grid_view.Rows[i].Cells[1].Value = computer.Name.ToString();
                                 IPHostEntry iphe 
= null;
                                 
try
                                 
{
                                     iphe 
= Dns.GetHostByName(computer.Name.ToString());
                                     
this.Grid_view.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString();
                                     ipAddress 
= iphe.AddressList[0].ToString();
                                     
this.Grid_view.Rows[i].Cells[3].Value = GetMacAddress();

                                 }

                                 
catch
                                 
{
                                     
continue;
                                 }

                                 
this.Grid_view.Rows[i].Cells[4].Value = domain.Name.ToString();
                                 i
++;
                             }
  
                           
                         }



                    
break;
            
            }

        }

    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值