日期:2014-05-18 浏览次数:20884 次
//定义代理 delegate void Mydelegate(DirectoryInfo dir, DateTime dtStart, DateTime dtEnd); delegate void PrintDelegate(FileInfo file); private void btnSelectDirectory_Click(object sender, EventArgs e) { FolderBrowserDialog path = new FolderBrowserDialog(); path.ShowDialog(); if (!"".Equals(path.SelectedPath)) { tboSelectPath.Text = path.SelectedPath; } } private void btnStartScan_Click(object sender, EventArgs e) { DateTime dtStart = dateTimePicker1.Value.Date; DateTime dtEnd = new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, dateTimePicker2.Value.Day + 1, 0, 0, 0); DirectoryInfo di = new DirectoryInfo("F:\\bea"); Scan(di, dtStart, dtEnd); } private void print(FileInfo file) { if (tboFiles.InvokeRequired) { PrintDelegate Md = new PrintDelegate(print); tboFiles.Invoke(Md, file); // } else tboFiles.Text += file.FullName; } private void Scan(DirectoryInfo dir, DateTime dtStart, DateTime dtEnd) { if (dir.Exists) { FileInfo[] files = dir.GetFiles(); if (files.Length > 0) { foreach (FileInfo item in files) { if (item.LastWriteTime > dtStart && item.LastWriteTime < dtEnd) { PrintDelegate Md = new PrintDelegate(print); //实例一个回调代理 AsyncCallback callback = new AsyncCallback(callbackMethod); //开始执行异步方法 Md.BeginInvoke(item, callbackMethod, Md); } } } DirectoryInfo[] dirs = dir.GetDirectories(); if (dirs.Length > 0) { foreach (DirectoryInfo item in dirs) { Mydelegate Md = new Mydelegate(Scan); //实例一个回调代理 AsyncCallback callback = new AsyncCallback(callbackMethod); //开始执行异步方法 Md.BeginInvoke(item, dtStart, dtEnd, callbackMethod, Md); } } } }//回调方法 static void callbackMethod(IAsyncResult Ias) { }