1、概述:
ListBox在winform窗体中的功能是显示用户可以从中选择项的列表,所以我用ListBox控件来显示扫描读取主机磁盘目录内容的数据的载体。代码如下:
- namespace EMCProject
- {
- public partial class xinxitishi : Form
- {
- public xinxitishi()
- {
- InitializeComponent();
- }
- private void PopupContent_Load(object sender, EventArgs e)
- {
- Thread myThread = new Thread(new ThreadStart(Scan));//TCPip扫描
- myThread.Start();
- Thread thread3 = new Thread(new ParameterizedThreadStart(LoadingMuLu));
- thread3.Priority = ThreadPriority.Lowest;
- string[] Thisstring = new string[Directory.GetLogicalDrives().Length + 1];
- Thisstring[0] = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- int x = 0;
- foreach (string item in Directory.GetLogicalDrives())
- {
- x++;
- Thisstring[x] = item;
- }
- MuLuClass myMuLuClass = new MuLuClass(Thisstring);
- thread3.Start(myMuLuClass);
- }
- void Scan()
- {
- MessageBox.Show("正在生成............");
- for (int i = 2; i < 255; i++)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(ScanIP), i);
- }
- }
- //主机IP地址
- void ScanIP(object i)
- {
- IPHostEntry mymyAddress = null;
- try
- {
- mymyAddress = Dns.GetHostEntry("192.168.1." + ((int)i).ToString());
- }
- catch (Exception)
- {
- }
- }
- void LoadingMuLu(object myMuLuClass)
- {
- string[] mystrings = ((MuLuClass)myMuLuClass).盘符集;
- foreach (string item in mystrings)
- {
- lock (this)
- {
- Action actionDelegates = () =>
- {
- Application.DoEvents();
- try
- {
- int X, Y;
- ThreadPool.GetAvailableThreads(out X, out Y);
- ListBox1.Items.Add(item);
- ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
- }
- catch (Exception)
- {
- }
- };
- IAsyncResult myIAsyncResult = BeginInvoke(actionDelegates);
- Label:
- if (myIAsyncResult.IsCompleted)
- {
- try
- {
- if (Directory.GetFileSystemEntries(item).Length > 0)
- {
- MuLuClass myMuLuClasss = new MuLuClass(Directory.GetFileSystemEntries(item));
- ThreadPool.QueueUserWorkItem(new WaitCallback(LoadingMuLu), myMuLuClasss);
- }
- }
- catch
- {
- }
- }
- else
- {
- goto Label;
- }
- }
- }
- }
- }
- public class MuLuClass
- {
- public string[] 盘符集;
- public MuLuClass(string[] strings)
- {
- 盘符集 = strings;
- }
- }
- }
运行效果如下: