form1.cs

来自「扫描网络中IP的网段」· CS 代码 · 共 89 行

CS
89
字号
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.Threading;

namespace 扫描网段
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void ScanNetwork()
        {
            listView1.Items.Clear();
            ListViewItem listViewItem;
            string strIPAddress = numericUpDown1.Text + "." + numericUpDown2.Text + "." + numericUpDown3.Text + ".";
            int start = Int32.Parse(numericUpDown4.Text);
            int end = Int32.Parse(numericUpDown5.Text);
            if (start > end)
                MessageBox.Show("终止地址必须大于起始地址", "提示");
            else
            {
                for (int i = start; i <= end; i++)
                {
                    string strScanAdd = strIPAddress + i.ToString();
                    IPAddress scanAdd = IPAddress.Parse(strScanAdd);
                    listViewItem = new ListViewItem(strScanAdd, 0);
                    try
                    {
                        IPHostEntry hostInfo = Dns.GetHostByAddress(scanAdd );
                        string hostName = hostInfo.HostName.ToString();
                        listViewItem.SubItems.Add(hostName );
                        listViewItem.SubItems.Add("可达");


                    }
                    catch (Exception exc)
                    {
                        listViewItem.SubItems.Add("未知");
                        listViewItem.SubItems.Add("不可达");
                    }
                    listView1.Items.Add(listViewItem );
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            currIP.Text = "";
            try
            {
                string hostName = Dns.GetHostName();
                IPHostEntry hostInfo = Dns.GetHostByName(hostName);
                IPAddress[] ipAddress = hostInfo.AddressList;
                foreach (IPAddress address in ipAddress)
                {
                    currIP.Text += address.ToString() + "";

                }
            }
            catch (Exception exc)
            { MessageBox.Show(exc.Message, "提示"); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread scan = new Thread(new ThreadStart (ScanNetwork ));
            scan.Start();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?