我有一个递归函数来搜索文件夹。
private int contFiles = 0;
private List<string> GetFiles(string folder, string filter)
{
var files = new List<string>();
Action<string> getFilesInDir = null;
getFilesInDir = new Action<string>(dir =>
{
contFiles++;
tslQuant.Text = contFiles.ToString(); //ToolStripItem
try
{
// get all the files in this directory
files.AddRange(Directory.GetFiles(dir, filter));
// and recursively visit the directories
foreach (var subdir in Directory.GetDirectories(dir))
{
getFilesInDir(subdir);
}
}
catch (UnauthorizedAccessException uae)
{
Console.WriteLine(uae.Message);
}
});
getFilesInDir(folder);
return files;
}该函数增加contFiles,并将该数字设置为ToolStripItem,但我总是得到"System.ArgumentOutOfRangeException“。
如何增加此值(最多5000)并在TSI?中显示
错误:
System.ArgumentOutOfRangeException是未处理的Message=“索引超出了范围。它必须是非负的并且小于集合的大小。参数名称:索引”。
System.Windows.Forms.ToolStripItemCollection.get_Item(Int32索引中的StackTrace:在System.Collections.ArrayList.get_Item中(Int32索引)在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e中),在System.Windows.Forms.ToolStrip.WndProc(Message& m中的System.Windows.Forms.Control.WmPaint(Message& m),在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m中的System.Windows.Forms.Control.WmPaint(Message& m) )中的System.Windows.Forms.Control.WmPaint(Message& m),在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m中的System.Windows.Forms.Control.WmPaint(Message& m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message& m)中的Int16层(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的Int16层(Message&m)中的Int16层(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的Int16层(Message&m)中的(Message&m)中的Int16层),在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd中的System.Windows.Forms.Control.WmPaint(Message&m)中的(Message&m)中的(Message&m)
编辑
在阅读了整个程序的代码之后,我注意到Fun广子是在Do_Work中扩展的,所以我使用了
backgroundWorker2.ReportProgress((1));去报到,一切都正常。
我不知道为什么,但不知怎么的,toolStripItem甚至可以在backgroundWorker、标签和其他控件中访问。
发布于 2013-05-20 19:08:42
您似乎从未将contFiles变量重置为零。如果你给GetFiles()打了几次电话,你可能会遇到麻烦。所以你可以在这里把它重新设置为零
contFiles = 0; // Reset variable to prevent overflow
var files = new List<string>();
Action<string> getFilesInDir = null;
getFilesInDir = new Action<string>(dir =>
{ ...https://stackoverflow.com/questions/16656308
复制相似问题