c# 獲得區域網路主機列表執行個體_C#教程

來源:互聯網
上載者:User
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;

namespace WindowLanSearch
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private string[,] LanHost;
  private System.Windows.Forms.ProgressBar progressBarSearch;
  private Thread[]  thread;
  private System.Windows.Forms.ListView listView1;
  private System.Windows.Forms.ColumnHeader columnHeader1;
  private System.Windows.Forms.ColumnHeader columnHeader2;
  private string str;
  /// <summary>
  /// 必需的設計器變數。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 表單設計器支援所必需的
   //
   InitializeComponent();
   InitLanHost();
   progressBarSearch.Maximum = 255;

   //
   // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
   //
  }

  /// <summary>
  /// 數組初始化
  /// </summary>
  private void InitLanHost()
  {
   LanHost = new string[255,2];
   for (int i=0;i<255;i++)
   {
    LanHost[i,0] = "";
    LanHost[i,1] = "";
   }
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 表單設計器產生的程式碼
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.progressBarSearch = new System.Windows.Forms.ProgressBar();
   this.listView1 = new System.Windows.Forms.ListView();
   this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
   this.SuspendLayout();
   // 
   // textBox1
   // 
   this.textBox1.Location = new System.Drawing.Point(24, 40);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
   this.textBox1.Size = new System.Drawing.Size(176, 296);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(456, 40);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "開始搜尋";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // progressBarSearch
   // 
   this.progressBarSearch.Location = new System.Drawing.Point(32, 360);
   this.progressBarSearch.Name = "progressBarSearch";
   this.progressBarSearch.Size = new System.Drawing.Size(490, 24);
   this.progressBarSearch.TabIndex = 2;
   // 
   // listView1
   // 
   this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                      this.columnHeader1,
                      this.columnHeader2});
   this.listView1.Location = new System.Drawing.Point(248, 40);
   this.listView1.Name = "listView1";
   this.listView1.Size = new System.Drawing.Size(184, 288);
   this.listView1.TabIndex = 5;
   // 
   // columnHeader1
   // 
   this.columnHeader1.Text = "dddd";
   // 
   // columnHeader2
   // 
   this.columnHeader2.Text = "sssss";
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(544, 413);
   this.Controls.Add(this.listView1);
   this.Controls.Add(this.progressBarSearch);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }
  private void button1_Click(object sender, System.EventArgs e)
  {

   LanSearch();

  }
  /// <summary>
  /// 區域網路搜尋事件
  /// </summary>
  private void LanSearch()
  {
   thread = new Thread[255];

   ThreadStart threadMethod;

   Thread threadProgress = new Thread(new ThreadStart(progressSearch));
   threadProgress.Start();

   string localhost = (Dns.GetHostByName(Dns.GetHostName())).AddressList[0].ToString();  //本地主機IP地址
   str = localhost.Substring(0,localhost.LastIndexOf("."));

   for (int i=0;i<255;i++)  //建立255個線程掃描IP
   {
    threadMethod = new ThreadStart(LanSearchThreadMethod);
    thread[i] = new Thread(threadMethod);
    thread[i].Name = i.ToString();
    thread[i].Start();
    if (!thread[i].Join(100))    //Thread.Join(100)不知道這處這麼用對不對,感覺沒什麼效果一樣
    {
     thread[i].Abort();
    }
   }

   GetLanHost();
   listLanHost();
  }
  /// <summary>
  /// 多線程搜尋方法
  /// </summary>
  private void LanSearchThreadMethod()
  {
   int Currently_i = Convert.ToUInt16(Thread.CurrentThread.Name);  //當前進程名稱

   IPAddress ScanIP = IPAddress.Parse( str + "."+Convert.ToString(Currently_i +1));  //獲得掃描IP地址
   IPHostEntry ScanHost = null;
   ScanHost = Dns.GetHostByAddress(ScanIP);   //獲得掃描IP地址主機資訊

   if (ScanHost != null)
   {
    LanHost[Currently_i,0] = ScanIP.ToString();
    LanHost[Currently_i,1] = ScanHost.HostName;
   }

   //progressBarSearch.Value = progressBarSearch.Value +1;

  }
  /// <summary>
  /// 文字框顯示主機名稱與IP列表
  /// </summary>
  private void GetLanHost()
  {
   for (int i=0;i<255;i++)
    if ( LanHost[i,0] !="")
    {
     textBox1.Text =textBox1.Text + LanHost[i,1] +":" +LanHost[i,0] + "\r\n";
    }
  }
  /// <summary>
  /// listview1 顯示搜尋主機
  /// </summary>
  private void listLanHost()
  {
   listView1.View = View.List;

   ListViewItem aa ;
   for (int i=0;i<255;i++)
   {
    if ( LanHost[i,0] !="")
    {
     aa= new ListViewItem();
     aa.Text = LanHost[i,1];
     aa.Tag = LanHost[i,0];
     listView1.Items.Add(aa);
    }
   }

  }
  /// <summary>
  /// 進度條處理線程
  /// </summary>
  private void progressSearch()
  {
   //label1.Text = "進度條只是時間估計,不是真實搜尋進度!";
   progressBarSearch.Value = 0;
   for (int i=0;i<255;i++)
   {
    progressBarSearch.Value = progressBarSearch.Value + 1;
    Thread.Sleep(100);
   }
  }
}
}

遺憾之處:因搜尋較慢,沒有實現真實的搜尋進度。
不懂之處:實現文字提示時,當在滑鼠事件首尾插入
private void button1_Click(object sender, System.EventArgs e)
  {
    lab1.Text = “開始搜尋”;      //新插入    
   LanSearch();
    lab1.Text = “結束搜尋”;     //新插入
  }
文本提示時,在lab1上始終不能及時顯示,而是等所有線程結束後才顯示“結束搜尋“。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.