我正在尝试在下面的函数中穿线,以便背景表单可以继续移动并且不会“冻结”,有人可以帮我这样做吗?
public IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (Exception ex)
{
richTextBox1.AppendText(ex.Message);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
richTextBox1.AppendText(ex.Message);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];
}
}
}
}
我使用以下按钮执行代码:
private void button1_Click(object sender, EventArgs e)
{
int count = 0;
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowDialog();
string selected = dialog.SelectedPath;
foreach (string file in GetFiles(selected))
{
count++;
richTextBox1.AppendText(file + Environment.NewLine);
}
label2.Text = count.ToString();
}