⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmexample0.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -