NetWork ScanIP Address Program
题目: 内部网络扫描程序
参考:http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Csharpnet.mspx
如下图:
实现功能:
1 当前本机的连机状态.
2 计算机名称--->IP;   IP--->计算机名称
3 网段扫描
实现过程:
需要调用的引用:
 using System;
using System; using System.Runtime ;
using System.Runtime ; using System.Runtime.InteropServices ;
using System.Runtime.InteropServices ; 

 namespace NetworkScan
namespace NetworkScan {
{ /// <summary>
 /// <summary> ///
 ///  /// </summary>
 /// </summary> public class InternetCS
 public class InternetCS {
 { //Creating the extern function
  //Creating the extern function  [DllImport("wininet.dll")]
  [DllImport("wininet.dll")]  private extern static bool InternetGetConnectedState(int Description,int ReservedValue);
  private extern static bool InternetGetConnectedState(int Description,int ReservedValue);  
  //Creating a function that uses the API function
  //Creating a function that uses the API function  public static bool IsConnectedToInternet()
  public static bool IsConnectedToInternet()  {
  {  int Desc=0;
   int Desc=0;  return InternetGetConnectedState(Desc,0);
   return InternetGetConnectedState(Desc,0);  }
  } }
 } }
}

这个引用实现过程是:监控当前本机的连网状态
为了实现时实监控,需要在主程序里面进行时实监控 ,主程序里面添加Timer控件,时间间隔是1000微秒
 private void timer1_Tick(object sender, System.EventArgs e)
private void timer1_Tick(object sender, System.EventArgs e) {
        { label11.Text=DateTime.Now.Hour.ToString()+":"+ DateTime.Now.Minute.ToString()+":"+DateTime.Now.Second.ToString();
            label11.Text=DateTime.Now.Hour.ToString()+":"+ DateTime.Now.Minute.ToString()+":"+DateTime.Now.Second.ToString(); if (textBox3.Text!="")
            if (textBox3.Text!="") {
            { button3.Enabled=false;
                button3.Enabled=false; }
            } else
            else {
            { button3.Enabled=true;
                button3.Enabled=true; }
            } bool b_IsConnection=InternetCS.IsConnectedToInternet();
            bool b_IsConnection=InternetCS.IsConnectedToInternet(); if (b_IsConnection)
            if (b_IsConnection) {
            { //
                // textBox3.BackColor=Color.Red;//联网状态
                textBox3.BackColor=Color.Red;//联网状态 }
            } else
            else {
            { textBox3.BackColor=Color.Yellow;  //非联网状态
                textBox3.BackColor=Color.Yellow;  //非联网状态 }
            } }
        }窗体Load事件里面,代码如下:
 private void Form1_Load(object sender, System.EventArgs e)
private void Form1_Load(object sender, System.EventArgs e) {
        { timer1.Enabled=true;
            timer1.Enabled=true; IPHostEntry myHost = new IPHostEntry();
            IPHostEntry myHost = new IPHostEntry(); try
            try {
            { getlocalipaddress.Text="";
                getlocalipaddress.Text=""; //得到本地主机的DNS信息
                //得到本地主机的DNS信息 myHost = Dns.GetHostByName(Dns.GetHostName());
                myHost = Dns.GetHostByName(Dns.GetHostName()); //显示本地主机名
                //显示本地主机名 getlocalname.Text = "你的计算机名称:"+myHost.HostName.ToString();
                getlocalname.Text = "你的计算机名称:"+myHost.HostName.ToString(); //显示本地主机的IP地址表
                //显示本地主机的IP地址表 getlocalipaddress.Text="";
                getlocalipaddress.Text=""; for(int i=0; i<myHost.AddressList.Length;i++)
                for(int i=0; i<myHost.AddressList.Length;i++) {
                { getlocalipaddress.Text="本地主机IP地址->"+myHost.AddressList[i].ToString();
                    getlocalipaddress.Text="本地主机IP地址->"+myHost.AddressList[i].ToString(); 
                     }
                } }
            } catch
            catch {
            { }
            } 
             
             }
        }IP--->ComputerName
 private void button2_Click(object sender, System.EventArgs e)
private void button2_Click(object sender, System.EventArgs e) {
        { //IP--->Name
            //IP--->Name label5.Text="";
            label5.Text=""; try
            try {
            { IPAddress test1 = IPAddress.Parse(textBox2.Text.Trim());
                IPAddress test1 = IPAddress.Parse(textBox2.Text.Trim()); IPHostEntry iphe = Dns.GetHostByAddress(test1);
                IPHostEntry iphe = Dns.GetHostByAddress(test1); label5.Text=iphe.HostName;
                label5.Text=iphe.HostName; }
            } catch
            catch {
            { if (label5.Text.Trim()=="")
                if (label5.Text.Trim()=="") {
                { label5.Text="没有找到对应的计算机名称";
                    label5.Text="没有找到对应的计算机名称"; }
                } }
            } }
        }
Name--->IP
 private void button1_Click_1(object sender, System.EventArgs e)
    private void button1_Click_1(object sender, System.EventArgs e) {
        { //name--->IP
            //name--->IP label1.Text="";
            label1.Text=""; try
            try {
            { 
                 IPHostEntry myDnsToIP = new IPHostEntry();
                IPHostEntry myDnsToIP = new IPHostEntry(); //Dns.Resolve 方法: 将 DNS 主机名或以点分隔的四部分表示法格式的 //  IP 地址解析为 IPHostEntry实例
                //Dns.Resolve 方法: 将 DNS 主机名或以点分隔的四部分表示法格式的 //  IP 地址解析为 IPHostEntry实例 myDnsToIP =Dns.Resolve(textBox1.Text.ToString());
                myDnsToIP =Dns.Resolve(textBox1.Text.ToString()); //显示此域名的IP地址的列表
                //显示此域名的IP地址的列表 for(int i=0;i<myDnsToIP.AddressList.Length;i++)
                for(int i=0;i<myDnsToIP.AddressList.Length;i++) {
                { label1.Text+=myDnsToIP.AddressList[i].ToString();
                    label1.Text+=myDnsToIP.AddressList[i].ToString(); }
                } 
                 }
            } catch
            catch  {
            { if (label1.Text.Trim()=="")
                if (label1.Text.Trim()=="") {
                { label1.Text="没有找到对应IP";
                    label1.Text="没有找到对应IP"; }
                }
 }
            } }
        }网段扫描部分:
 private void button3_Click(object sender, System.EventArgs e)
        private void button3_Click(object sender, System.EventArgs e) {
        { 
             int i_IpBegin=0;
            int i_IpBegin=0; int i_IpEnd=0;
            int i_IpEnd=0; try
            try {
            { i_IpBegin=int.Parse(textBox7.Text.Trim());
                i_IpBegin=int.Parse(textBox7.Text.Trim()); i_IpEnd=int.Parse(textBox8.Text.Trim());
                i_IpEnd=int.Parse(textBox8.Text.Trim()); if ((i_IpBegin<0)&&(i_IpEnd>255))
                if ((i_IpBegin<0)&&(i_IpEnd>255)) {
                { MessageBox.Show("输出超出范围");
                    MessageBox.Show("输出超出范围"); return;
                    return; }
                } }
            } catch
            catch  {
            { MessageBox.Show("输入错误");
                MessageBox.Show("输入错误"); return;
                return; }
            }
 textBox4.Text="";
            textBox4.Text=""; //Thread 类: 创建并控制线程
            //Thread 类: 创建并控制线程 Thread thScan = new Thread(new ThreadStart(ScanTarget));
            Thread thScan = new Thread(new ThreadStart(ScanTarget)); //Thread.Start 方法:启动线程
            //Thread.Start 方法:启动线程 thScan.Start();
            thScan.Start(); 
            
             }
        } private void ScanTarget()
        private void ScanTarget() {
        { //构造IP地址的31-8BIT 位,也就是固定的IP地址的前段
            //构造IP地址的31-8BIT 位,也就是固定的IP地址的前段 // numericUpDown1是定义的System.Windows.Forms.NumericUpDown控件
            // numericUpDown1是定义的System.Windows.Forms.NumericUpDown控件 int i_IpBegin=int.Parse(textBox7.Text.Trim());
            int i_IpBegin=int.Parse(textBox7.Text.Trim()); int i_IpEnd=int.Parse(textBox8.Text.Trim());
            int i_IpEnd=int.Parse(textBox8.Text.Trim()); string strIPAddress=textBox5.Text;
            string strIPAddress=textBox5.Text; //扫描的操作
            //扫描的操作 for(int i=i_IpBegin;i<=i_IpEnd;i++)
            for(int i=i_IpBegin;i<=i_IpEnd;i++) {
            { string strScanIPAdd = strIPAddress +i.ToString();
                string strScanIPAdd = strIPAddress +i.ToString(); //转换成IP地址
                //转换成IP地址 IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
                IPAddress myScanIP = IPAddress.Parse(strScanIPAdd); textBox3.Text=i.ToString();
                textBox3.Text=i.ToString(); try
                try {
                {         //Dns.GetHostByAddress 方法: 根据 IP 地
                    //Dns.GetHostByAddress 方法: 根据 IP 地 //址获取 DNS 主机信息。
                    //址获取 DNS 主机信息。 IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
                    IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP); //获取主机的名
                    //获取主机的名 string strHostName =myScanHost.HostName.ToString();
                    string strHostName =myScanHost.HostName.ToString(); textBox4.Text+=strScanIPAdd+"->"+strHostName+"\r\n";
                    textBox4.Text+=strScanIPAdd+"->"+strHostName+"\r\n"; }
                } catch //(Exception error)
                catch //(Exception error) {
                { //MessageBox.Show(error.Message);
                    //MessageBox.Show(error.Message);                     }
                } }
            } 
 textBox3.Text="";
            textBox3.Text="";   MessageBox.Show("扫描完成");
            MessageBox.Show("扫描完成"); }
        }
 private void Form1_Closed(object sender, System.EventArgs e)
        private void Form1_Closed(object sender, System.EventArgs e) {
        { Application.Exit();
            Application.Exit();  }
        }
 private void textBox3_TextChanged(object sender, System.EventArgs e)
        private void textBox3_TextChanged(object sender, System.EventArgs e) {
        { 
         }
        } 
         private void ShowHideWindow(bool isShow)
        private void ShowHideWindow(bool isShow) {
        { if (isShow)
            if (isShow) {
            { if (this.ShowInTaskbar==false)
                if (this.ShowInTaskbar==false) {
                { this.ShowInTaskbar=true;
                    this.ShowInTaskbar=true; this.Show();
                    this.Show(); this.WindowState=FormWindowState.Normal;
                    this.WindowState=FormWindowState.Normal; }
                } else
                else {
                { if(this.WindowState == FormWindowState.Minimized)
                    if(this.WindowState == FormWindowState.Minimized) {
                    { this.WindowState = FormWindowState.Normal;
                        this.WindowState = FormWindowState.Normal; }
                    } }
                } this.Activate();
                this.Activate(); }
            } else
            else {
            { if (this.ShowInTaskbar==true)
                if (this.ShowInTaskbar==true) {
                { this.Hide();
                    this.Hide(); this.ShowInTaskbar=false;
                    this.ShowInTaskbar=false; }
                } }
            } }
        } }
    }
使用的外部类:
using System.IO;
using System.Net;
using System.Threading;
using System.Text.RegularExpressions;
程序源码下载地址:
https://files.cnblogs.com/jhtchina/NetworkScan.rar
 
                    
                

 
  
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号