ListBox扫描读取主机磁盘目录

本文介绍了一个Windows Forms应用程序,使用ListBox控件来显示通过扫描读取的主机磁盘目录内容。在`PopupContent_Load`事件中启动了两个线程,一个用于TCP扫描,另一个用于加载目录。代码遍历所有逻辑驱动器,并将其添加到ListBox中,同时检查每个目录是否存在子文件系统并递归加载。
摘要由CSDN通过智能技术生成

1概述

ListBox在winform窗体中的功能是显示用户可以从中选择项的列表,所以我用ListBox控件来显示扫描读取主机磁盘目录内容的数据的载体。代码如下:

  1. namespace EMCProject  
  2. {  
  3.     public partial class xinxitishi : Form  
  4.     {  
  5.         public xinxitishi()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.   
  10.         private void PopupContent_Load(object sender, EventArgs e)  
  11.         {  
  12.             Thread myThread = new Thread(new ThreadStart(Scan));//TCPip扫描  
  13.             myThread.Start();  
  14.             Thread thread3 = new Thread(new ParameterizedThreadStart(LoadingMuLu));  
  15.             thread3.Priority = ThreadPriority.Lowest;  
  16.             string[] Thisstring = new string[Directory.GetLogicalDrives().Length + 1];  
  17.             Thisstring[0] = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);  
  18.             int x = 0;  
  19.             foreach (string item in Directory.GetLogicalDrives())  
  20.             {  
  21.                 x++;  
  22.                 Thisstring[x] = item;  
  23.             }  
  24.             MuLuClass myMuLuClass = new MuLuClass(Thisstring);  
  25.             thread3.Start(myMuLuClass);  
  26.   
  27.         }  
  28.         void Scan()  
  29.         {  
  30.             MessageBox.Show("正在生成............");  
  31.             for (int i = 2; i < 255; i++)  
  32.             {  
  33.                 ThreadPool.QueueUserWorkItem(new WaitCallback(ScanIP), i);  
  34.             }  
  35.         }  
  36.   
  37.         //主机IP地址  
  38.         void ScanIP(object i)  
  39.         {  
  40.             IPHostEntry mymyAddress = null;  
  41.             try  
  42.             {  
  43.                 mymyAddress = Dns.GetHostEntry("192.168.1." + ((int)i).ToString());  
  44.             }  
  45.             catch (Exception)  
  46.             {  
  47.             }  
  48.         }  
  49.         void LoadingMuLu(object myMuLuClass)  
  50.         {  
  51.             string[] mystrings = ((MuLuClass)myMuLuClass).盘符集;  
  52.             foreach (string item in mystrings)  
  53.             {  
  54.                 lock (this)  
  55.                 {  
  56.                     Action actionDelegates = () =>  
  57.                     {  
  58.                         Application.DoEvents();  
  59.                         try  
  60.                         {  
  61.                             int X, Y;  
  62.                             ThreadPool.GetAvailableThreads(out X, out Y);    
  63.                             ListBox1.Items.Add(item);  
  64.                             ListBox1.SelectedIndex = ListBox1.Items.Count - 1;  
  65.                         }  
  66.                         catch (Exception)  
  67.                         {  
  68.                         }  
  69.                     };  
  70.                     IAsyncResult myIAsyncResult = BeginInvoke(actionDelegates);  
  71.                     Label:  
  72.                     if (myIAsyncResult.IsCompleted)  
  73.                     {  
  74.                         try  
  75.                         {  
  76.                             if (Directory.GetFileSystemEntries(item).Length > 0)  
  77.                             {  
  78.                                 MuLuClass myMuLuClasss = new MuLuClass(Directory.GetFileSystemEntries(item));  
  79.                                 ThreadPool.QueueUserWorkItem(new WaitCallback(LoadingMuLu), myMuLuClasss);  
  80.                             }  
  81.                         }  
  82.                         catch  
  83.                         {  
  84.                         }  
  85.                     }  
  86.                     else  
  87.                     {  
  88.                         goto Label;  
  89.                     }  
  90.                 }  
  91.             }  
  92.         }  
  93.     }  
  94.   
  95.     public class MuLuClass  
  96.     {  
  97.         public string[] 盘符集;  
  98.         public MuLuClass(string[] strings)  
  99.         {  
  100.             盘符集 = strings;  
  101.         }  
  102.     }  
  103. }  

运行效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值