我目前有代碼來遍歷我的計算機上的文件,雖然我試圖掛鉤Button.Click事件來執行此操作,我將如何做到這一點?輸出到哪裏去?下面C#迭代通過文件幫助
代碼:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
public class FileSystemList : IEnumerable<string>
{
DirectoryInfo rootDirectory;
public FileSystemList(string root)
{
rootDirectory = new DirectoryInfo(root);
}
public IEnumerator<string> GetEnumerator()
{
return ProcessDirectory(rootDirectory).GetEnumerator();
}
public IEnumerable<string> ProcessDirectory(DirectoryInfo dir)
{
yield return dir.FullName;
foreach (FileInfo file in dir.EnumerateFiles())
yield return file.FullName;
foreach (DirectoryInfo subdir in dir.EnumerateDirectories())
foreach (string result in ProcessDirectory(subdir))
yield return result;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
你能舉個例子嗎? – James 2011-05-21 18:57:55