frmexample0.cs

来自「Microsoft Mobile Development Handbook的代码」· CS 代码 · 共 87 行

CS
87
字号
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Threading;

namespace CodeForChapter11cs
{
  public partial class frmExample0 : Form
  {
    public frmExample0()
    {
      InitializeComponent();
    }

    private ArrayList allFiles = new ArrayList(); // holds the results
    private bool stopRequested = false;           // user hit the cancel button

    private void menuItem2_Click(object sender, EventArgs e)
    {
      // cancel
      stopRequested = true;
    }

    private void menuItem1_Click(object sender, EventArgs e)
    {
      // load list
      label1.Text = "";
      listBox1.DataSource = null;
      stopRequested = false;

      this.GetAllFiles(); //will have populated the allFiles ArrayList

      label1.Text = allFiles.Count.ToString();
      listBox1.DataSource = allFiles;
      Debug.WriteLine("Got results " + listBox1.Items.Count.ToString());
    }

    private void GetAllFiles()
    {
      allFiles.Clear();
      this.PopulateAllFilesFor(@"\");
      Debug.WriteLine("Finished");
    }

    private void PopulateAllFilesFor(string path)
    {
      #region non-ideal solutions
      // this.Refresh();          //Non-ideal solution 1
      // Application.DoEvents();  //Non-ideal solution 2      
      #endregion

      if (stopRequested)
      {
        Debug.WriteLine("cancelled 1");
        return;
      }
      Debug.WriteLine("Processing new path");

      allFiles.Add(path);

      string[] files;
      files = Directory.GetFiles(path);
      allFiles.AddRange(files);

      Debug.WriteLine(allFiles.Count.ToString());
      label1.Text = allFiles.Count.ToString();

      if (stopRequested)
      {
        Debug.WriteLine("cancelled 2");
        return;
      }

      foreach (string subDirectory in Directory.GetDirectories(path))
      {
        this.PopulateAllFilesFor(subDirectory); //recursion
        if (stopRequested)
        {
          Debug.WriteLine("cancelled 3");
          return;
        }
      }
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?