在这两个方法GetDirectories和MyGetDirectories我正在递归所有的目录和子目录。 我想在progresschanged事件中的label2上显示,就像计数器一样,计算目录的数量直到完成。
private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) { MySubDirectories = GetDirectories(BasePath).ToArray(); } private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }然后是GetDirectories方法
private List<DirectoryInfo> GetDirectories(string basePath) { IEnumerable<string> str = MyGetDirectories(basePath); List<DirectoryInfo> l = new List<DirectoryInfo>(); l.Add(new DirectoryInfo(basePath)); IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); l.AddRange(dirs); return l; }和方法MyGetDirectories
private static IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } }我尝试的是MyGetDirectories方法我添加了一个名为变量countDirectories的计数器,我检查它确实计数。
static int countDirectories = 0; private static IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); countDirectories = countDirectories + dirs.Length; return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } }然后在DoWork活动中我做了:
private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) { MySubDirectories = GetDirectories(BasePath).ToArray(); _FileInformationWorker.ReportProgress(0,countDirectories.ToString()); }并在progresschanged活动中
private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { label2.Text = e.UserState.ToString(); }但它并没有向label2报告,因为它仍然在GetDirectories方法中工作。 所以我被困在这里。
In this two methods GetDirectories and MyGetDirectories i'm getting recursive all the directories and sub directories. I want to display on label2 in the progresschanged event like a counter that count the number of directories until it finish.
private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) { MySubDirectories = GetDirectories(BasePath).ToArray(); } private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }Then the GetDirectories method
private List<DirectoryInfo> GetDirectories(string basePath) { IEnumerable<string> str = MyGetDirectories(basePath); List<DirectoryInfo> l = new List<DirectoryInfo>(); l.Add(new DirectoryInfo(basePath)); IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); l.AddRange(dirs); return l; }And the method MyGetDirectories
private static IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } }What I tried is in the method MyGetDirectories I added a counter called the variable countDirectories and I checked it does count.
static int countDirectories = 0; private static IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); countDirectories = countDirectories + dirs.Length; return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } }Then in the DoWork event I did:
private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) { MySubDirectories = GetDirectories(BasePath).ToArray(); _FileInformationWorker.ReportProgress(0,countDirectories.ToString()); }And in the progresschanged event
private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { label2.Text = e.UserState.ToString(); }But it's not reporting to the label2 anything I guess since it's still working inside the GetDirectories method. So i'm stuck here.
最满意答案
这是只有按钮和标签的表单代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DirectoryInfo[] MySubDirs; BackgroundWorker w; int count; private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; count = 0; w = new BackgroundWorker(); w.DoWork += w_DoWork; w.ProgressChanged += w_ProgressChanged; w.WorkerReportsProgress = true; w.RunWorkerCompleted += w_RunWorkerCompleted; w.RunWorkerAsync(); } void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { button1.Enabled = true; } void w_ProgressChanged(object sender, ProgressChangedEventArgs e) { label1.Text = e.UserState.ToString(); } void w_DoWork(object sender, DoWorkEventArgs e) { MySubDirs = GetDirectories("d:\\prj").ToArray(); } private List<DirectoryInfo> GetDirectories(string basePath) { IEnumerable<string> str = MyGetDirectories(basePath); List<DirectoryInfo> l = new List<DirectoryInfo>(); l.Add(new DirectoryInfo(basePath)); IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); l.AddRange(dirs); return l; } //not static so we can report progress from it private IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); count += dirs.Length; w.ReportProgress(0, count.ToString()); return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } } } }This is code of form with just button and label on it:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DirectoryInfo[] MySubDirs; BackgroundWorker w; int count; private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; count = 0; w = new BackgroundWorker(); w.DoWork += w_DoWork; w.ProgressChanged += w_ProgressChanged; w.WorkerReportsProgress = true; w.RunWorkerCompleted += w_RunWorkerCompleted; w.RunWorkerAsync(); } void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { button1.Enabled = true; } void w_ProgressChanged(object sender, ProgressChangedEventArgs e) { label1.Text = e.UserState.ToString(); } void w_DoWork(object sender, DoWorkEventArgs e) { MySubDirs = GetDirectories("d:\\prj").ToArray(); } private List<DirectoryInfo> GetDirectories(string basePath) { IEnumerable<string> str = MyGetDirectories(basePath); List<DirectoryInfo> l = new List<DirectoryInfo>(); l.Add(new DirectoryInfo(basePath)); IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); l.AddRange(dirs); return l; } //not static so we can report progress from it private IEnumerable<string> MyGetDirectories(string basePath) { try { string[] dirs = Directory.GetDirectories(basePath); count += dirs.Length; w.ReportProgress(0, count.ToString()); return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); } catch (UnauthorizedAccessException) { return Enumerable.Empty<string>(); } } } }更多推荐
发布评论