Scan Computer

namespace ScanComputer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear ( ) ; //清楚扫描结果显示区域

           DateTime StartTime = DateTime.Now ;  //获取当前时间

           string mask = numericUpDown1.Value.ToString ( ) + "." + numericUpDown2.Value.ToString ( ) +

            "." + numericUpDown3.Value.ToString ( ) + "." ;

           int Min = ( int ) numericUpDown3.Value ;

           int Max = ( int ) numericUpDown4.Value ;

           if ( Min > Max )

           {

                MessageBox.Show ( "Illegal IP address,please check!" , "Error!" ) ;

                return ;

            }

           //判断输入的IP地址区间是否合法

           progressBar1.Minimum = Min ;

           progressBar1.Maximum = Max ;

            int i ;

            for ( i = Min ; i <= Max ; i++ )

            {

                string ip= mask + i.ToString ( ) ;

                IPAddress myIP = IPAddress.Parse ( ip ) ;

                //根据给定的IP地址字符串,处境IPAddress实例

                try

                {

                   IPHostEntry myHost = Dns.GetHostEntry( myIP ) ;

                   string HostName = myHost.HostName.ToString ( ) ;

                   listBox1.Items.Add ( ip + "Name is:" + HostName ) ;  //添加listbox中项目的方法

                }

                catch

                {

                listBox1.Items.Add ( ip + " Host doesn’t response!" ) ;

                }

            progressBar1.Value = i ;

           }

           //扫描给定IP地址对应的计算机是否在线

           DateTime EndTime = DateTime.Now ;

           TimeSpan ts = EndTime-StartTime ;  //timespan类代表时间差

           //获得扫描网络计算机所使用的时间

           label4.Text = ts.Seconds.ToString ( ) + " seconds" ;

           MessageBox.Show ( "Checking has finished" , "Attention" ) ;

           progressBar1.Value = Min ;

//本文来自: 乘风原创程序(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/224/24023.htm
        }
    }
}

 

image

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s