C#扫描指定网段的计算机名称及MAC地址

[ Tuesday, March 11, 2008 ]
Tags:  C#  扫描MAC地址  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Runtime.InteropServices;
namespace NetMAC
{
    public partial class Form1 : Form
    {
        [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 delegate void SetTextCallback(string MAC, string ip, string name);
        private delegate void AddProess();
        private string Macstring = null;
        private string Fip = null ;
        private int ipNum;
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            Macstring = null;
            Fip = null;
            Netprogress.Value = 0;
            try
            {
                IPAddress ip1 = IPAddress.Parse(textBox1.Text);
                IPAddress ip2 = IPAddress.Parse(textBox2.Text);
            }
          catch(Exception ei)
            {
                MessageBox.Show("扫描的IP地址有误!","NetMac");
                return;
            }
            FormatIP(textBox1.Text);
            FormatIP(textBox2.Text);
            if(Convert.ToInt32(textBox2.Text.Substring(ipNum,textBox2.Text.Length-ipNum))
                - Convert.ToInt32(textBox1.Text.Substring(ipNum,textBox1.Text.Length-ipNum)) < 0)
            {
                return ;
            }
            button1.Enabled = false;
            ProessScan(Convert.ToInt32(textBox1.Text.Substring(ipNum,textBox1.Text.Length-ipNum))
              ,Convert.ToInt32(textBox2.Text.Substring(ipNum,textBox2.Text.Length-ipNum)));
        }
        private void FormatIP(string IPaddress)  //检查IP网段
        {
            for (int i = 1; i < IPaddress.Length; i++)
            {
                if (Convert.ToChar(IPaddress.Substring(i - 1, 1)) == '.')
                {
                    ipNum = i;
                }
            }
            if (Fip != null)
            {
                if (Fip != IPaddress.Remove(ipNum))
                {
                    MessageBox.Show("扫描的网段不一致!", "NetMac");
                    return;
                }
            }
            else
            {
                Fip = IPaddress.Remove(ipNum);
            }

        }
        private void ProessScan(int startip, int ProessNum)
        {            
                Netprogress.Maximum = ProessNum - startip + 1;
            for (int i = startip; i <= ProessNum; i++)
            {
                BackgroundWorker Bgw = new BackgroundWorker();
              Bgw.DoWork += new DoWorkEventHandler(Bgw_DoWork);
              Bgw.RunWorkerAsync(Fip + i.ToString());
            }
          
        }
        private void Bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            ScanMAC(e.Argument.ToString());
            ScanHostName(e.Argument.ToString());
            AddProess1();
        }
        private void AddProess1()
        {
            if (Netprogress.InvokeRequired)
            {
                Netprogress.Invoke(new AddProess(AddProess1));
            }
            else
            {
                Netprogress.Value = Netprogress.Value + 1;
            }
            if (Netprogress.Value == Netprogress.Maximum)
            {
                button1.Enabled = true;
            }
        }

        private void ScanMAC(string destIP)  //扫描MAC地址
        {
            try
            {
                IPHostEntry myip =Dns.GetHostByName(Dns.GetHostName());
                IPAddress MyAddress = new IPAddress(myip.AddressList[0].Address);
                Int32 ldest = inet_addr(destIP);
                Int32 lhost = inet_addr(MyAddress.ToString());
                Int64 mac = new Int64();
                Int32 len = 6;
                int ii = SendARP(ldest, lhost, ref  mac, ref  len);
                for (int i = 10; i >= 0; i = i - 2)
                {
                    Macstring = Macstring + mac.ToString("X4").Substring(i, 2) + "-";
                }
            }
            catch (Exception em)
            {
            }

        }
        private void ScanHostName(string hostIP) //扫描主机名字
        {
            try
            {
                IPHostEntry IPEntry = Dns.GetHostByAddress(hostIP);
                ListMac(Macstring.Remove(17), hostIP, IPEntry.HostName);
                Macstring = null;
            }
            catch (Exception eh)
            {
                
            }
        }
        private void ListMac(string MAC, string ip, string name)  //添加到列表
        {
            if (listView1.InvokeRequired)
            {
                listView1.Invoke(new SetTextCallback(ListMac), new object[] { MAC, ip, name });
            }
            else
            {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = MAC;
                li.SubItems.Add(ip);
                li.SubItems.Add(name);
                listView1.Items.Add(li);
            }
        }
    }
}
 

代码下载:1203832207_91200ef3.rar

相关文章:
发布:babycrazy | 分类:C# WinForm | 评论:0 | 引用:0 | 浏览:
发表评论