題目: 内部網絡掃描程式
參考:http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Csharpnet.mspx
如下圖:
實作功能:
1 目前本機的連機狀态.
2 計算機名稱--->IP; IP--->計算機名稱
3 網段掃描
實作過程:
需要調用的引用:
using System;
using System.Runtime ;
using System.Runtime.InteropServices ;
namespace NetworkScan
{
/**//// <summary>
///
/// </summary>
public class InternetCS
{
//Creating the extern function
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(int Description,int ReservedValue);
//Creating a function that uses the API function
public static bool IsConnectedToInternet()
{
int Desc=0;
return InternetGetConnectedState(Desc,0);
}
}
}
這個引用實作過程是:監控目前本機的連網狀态
為了實作時實監控,需要在主程式裡面進行時實監控 ,主程式裡面添加Timer控件,時間間隔是1000微秒
private void timer1_Tick(object sender, System.EventArgs e)
{
label11.Text=DateTime.Now.Hour.ToString()+":"+ DateTime.Now.Minute.ToString()+":"+DateTime.Now.Second.ToString();
if (textBox3.Text!="")
{
button3.Enabled=false;
}
else
button3.Enabled=true;
bool b_IsConnection=InternetCS.IsConnectedToInternet();
if (b_IsConnection)
//
textBox3.BackColor=Color.Red;//聯網狀态
textBox3.BackColor=Color.Yellow; //非聯網狀态
}
窗體Load事件裡面,代碼如下:
private void Form1_Load(object sender, System.EventArgs e)
timer1.Enabled=true;
IPHostEntry myHost = new IPHostEntry();
try
getlocalipaddress.Text="";
//得到本地主機的DNS資訊
myHost = Dns.GetHostByName(Dns.GetHostName());
//顯示本地主機名
getlocalname.Text = "你的計算機名稱:"+myHost.HostName.ToString();
//顯示本地主機的IP位址表
for(int i=0; i<myHost.AddressList.Length;i++)
{
getlocalipaddress.Text="本地主機IP位址->"+myHost.AddressList[i].ToString();
}
catch
IP--->ComputerName
private void button2_Click(object sender, System.EventArgs e)
//IP--->Name
label5.Text="";
IPAddress test1 = IPAddress.Parse(textBox2.Text.Trim());
IPHostEntry iphe = Dns.GetHostByAddress(test1);
label5.Text=iphe.HostName;
if (label5.Text.Trim()=="")
label5.Text="沒有找到對應的計算機名稱";
Name--->IP
private void button1_Click_1(object sender, System.EventArgs e)
//name--->IP
label1.Text="";
IPHostEntry myDnsToIP = new IPHostEntry();
//Dns.Resolve 方法: 将 DNS 主機名或以點分隔的四部分表示法格式的 // IP 位址解析為 IPHostEntry執行個體
myDnsToIP =Dns.Resolve(textBox1.Text.ToString());
//顯示此域名的IP位址的清單
for(int i=0;i<myDnsToIP.AddressList.Length;i++)
label1.Text+=myDnsToIP.AddressList[i].ToString();
catch
if (label1.Text.Trim()=="")
label1.Text="沒有找到對應IP";
網段掃描部分:
private void button3_Click(object sender, System.EventArgs e)
int i_IpBegin=0;
int i_IpEnd=0;
i_IpBegin=int.Parse(textBox7.Text.Trim());
i_IpEnd=int.Parse(textBox8.Text.Trim());
if ((i_IpBegin<0)&&(i_IpEnd>255))
MessageBox.Show("輸出超出範圍");
return;
MessageBox.Show("輸入錯誤");
return;
textBox4.Text="";
//Thread 類: 建立并控制線程
Thread thScan = new Thread(new ThreadStart(ScanTarget));
//Thread.Start 方法:啟動線程
thScan.Start();
private void ScanTarget()
//構造IP位址的31-8BIT 位,也就是固定的IP位址的前段
// numericUpDown1是定義的System.Windows.Forms.NumericUpDown控件
int i_IpBegin=int.Parse(textBox7.Text.Trim());
int i_IpEnd=int.Parse(textBox8.Text.Trim());
string strIPAddress=textBox5.Text;
//掃描的操作
for(int i=i_IpBegin;i<=i_IpEnd;i++)
string strScanIPAdd = strIPAddress +i.ToString();
//轉換成IP位址
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
textBox3.Text=i.ToString();
try
{
//Dns.GetHostByAddress 方法: 根據 IP 地
//址擷取 DNS 主機資訊。
IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
//擷取主機的名
string strHostName =myScanHost.HostName.ToString();
textBox4.Text+=strScanIPAdd+"->"+strHostName+"\r\n";
catch //(Exception error)
//MessageBox.Show(error.Message);
}
textBox3.Text="";
MessageBox.Show("掃描完成");
private void Form1_Closed(object sender, System.EventArgs e)
Application.Exit();
private void textBox3_TextChanged(object sender, System.EventArgs e)
private void ShowHideWindow(bool isShow)
if (isShow)
if (this.ShowInTaskbar==false)
this.ShowInTaskbar=true;
this.Show();
this.WindowState=FormWindowState.Normal;
else
if(this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
if (this.ShowInTaskbar==true)
this.Hide();
this.ShowInTaskbar=false;
}
使用的外部類:
using System.IO;
using System.Net;
using System.Threading;
using System.Text.RegularExpressions;