c#获得局域网主机列表实例
作者:yzxnha_975 | 来源:互联网 | 2022-03-05 00:27
c#获得局域网主机列表实例
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 { /// /// Form1 的摘要说明。 /// 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; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); InitLanHost(); progressBarSearch.Maximum = 255; // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 数组初始化 /// private void InitLanHost() { LanHost = new string[255,2]; for (int i=0;i<255;i++) { LanHost[i,0] = ""; LanHost[i,1] = ""; } } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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 /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { LanSearch(); } /// /// 局域网搜索事件 /// 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(); } /// /// 多线程搜索方法 /// 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; } /// /// 文本框显示主机名与IP列表 /// 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"; } } /// /// listview1 显示搜索主机 /// 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); } } } /// /// 进度条处理线程 /// 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上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
推荐阅读
本文详细介绍了Unity3D引擎的体系结构和功能。Unity3D是一个屡获殊荣的工具,用于创建交互式3D应用程序。它由游戏引擎和编辑器组成,支持C#、Boo和JavaScript脚本编程。该引擎涵盖了声音、图形、物理和网络功能等主题。Unity编辑器具有多语言脚本编辑器和预制装配系统等特点。本文还介绍了Unity的许可证情况。Unity基本功能有限的免费,适用于PC、MAC和Web开发。其他平台或完整的功能集需要购买许可证。 ...
[详细]
蜡笔小新 2023-12-10 11:01:31
本文介绍了Win10下游戏不能全屏的解决方法,包括修改注册表默认值和查看兼容游戏列表。同时提供了部分已经支持Win10的热门游戏列表,帮助玩家解决游戏不能全屏的问题。 ...
[详细]
蜡笔小新 2023-12-14 13:40:42
本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ...
[详细]
蜡笔小新 2023-12-12 19:37:50
本文介绍了一个免费的asp.net控件,该控件具备数据显示、录入、更新、删除等功能。它比datagrid更易用、更实用,同时具备多种功能,例如属性设置、数据排序、字段类型格式化显示、密码字段支持、图像字段上传和生成缩略图等。此外,它还提供了数据验证、日期选择器、数字选择器等功能,以及防止注入攻击、非本页提交和自动分页技术等安全性和性能优化功能。最后,该控件还支持字段值合计和数据导出功能。总之,该控件功能强大且免费,适用于asp.net开发。 ...
[详细]
蜡笔小新 2023-12-11 09:41:26
本文介绍了在开发过程中遇到JDK版本冲突的情况,以及如何通过修改环境变量实现JDK版本的切换功能,解决开发环境冲突的问题。通过合理的切换环境,可以更好地进行项目开发。同时,提醒读者注意不仅限于1.7和1.8版本的转换,还要适应不同项目和个人开发习惯的需求。 ...
[详细]
蜡笔小新 2023-12-10 19:24:11
本文介绍了如何通过注册表编辑器更改电脑系统的自动校时服务器地址。通过修改注册表中的数值数据或新建字符串数值的方式,可以将默认的时钟同步服务器地址更改为自己所需要的域名或IP地址。详细步骤包括双击时间区域,点击internet时间,勾选自动校正域名设置定时等操作。 ...
[详细]
蜡笔小新 2023-12-10 18:48:48
本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ...
[详细]
蜡笔小新 2023-12-10 16:03:24
本文介绍了在chrome浏览器中使用编辑器实现网页截图功能的方法。通过在地址栏中输入特定命令,打开控制台并调用命令面板,用户可以方便地进行网页截图操作。 ...
[详细]
蜡笔小新 2023-12-10 15:50:00
本文介绍了Windows Vista操作系统中的用户账户保护功能,该功能是为了增强系统的安全性而设计的。通过对Vista测试版的体验,可以看到系统在安全性方面的进步。该功能的引入,为用户的账户安全提供了更好的保障。 ...
[详细]
蜡笔小新 2023-12-10 11:22:01
本文介绍了在Linux中执行.sh脚本时出现/bin/sh^M: bad interpreter: No such file or directory异常的原因分析,并提供了两种解决方法:在Windows下进行编码格式转换,或在Linux中修改文件格式和执行权限。具体操作步骤也在摘要中给出。 ...
[详细]
蜡笔小新 2023-12-09 10:15:09
本文介绍了iOS开发中检测和解决内存泄漏的方法,包括静态分析、使用instruments检查内存泄漏以及代码测试等。同时还介绍了最能挣钱的行业,包括互联网行业、娱乐行业、教育行业、智能行业和老年服务行业,并提供了选行业的技巧。 ...
[详细]
蜡笔小新 2023-12-09 10:07:05
一、修改注册表去掉桌面图标小箭头1按下win+R组合快捷键,打开windows10系统的“运行”窗口,输入“regedit”,打开注册表编辑器,找到HKEY_CLASSES_ROOT\lnkfi ...
[详细]
蜡笔小新 2023-10-17 19:17:08
3.3自定义快捷键(CustomizingHotkeys)ChemDraw快捷键由一个XML文件定义,我们可以根据自己的需要, ...
[详细]
蜡笔小新 2023-10-17 19:10:46
今天打开NavicatPremium,突然中文都变成乱码了,如下所示。我的版本是12.0.24(64-bit)Premium ...
[详细]
蜡笔小新 2023-10-17 19:09:25
本文由编程笔记#小编为大家整理,主要介绍了QML编码约定相关的知识,希望对你有一定的参考价值。 ...
[详细]
蜡笔小新 2023-10-17 18:33:35
yzxnha_975
这个家伙很懒,什么也没留下!