我目前正在处理一段代码,它循环遍历给定路径中的所有文件,包括子目录,并计算该路径中具有特定文件扩展名的文件数。我的代码部分似乎工作正常。
问题来自于我决定添加一些额外的功能,在程序计算扩展时提供更新文本,因为有时可能需要一点时间来完成搜索所有子目录。
所以我尝试添加一些东西,这些东西会在给定的时间间隔内更改listview的给定行中的文本,以表示类似“处理”的内容。 “处理......”处理......“处理......”等,每隔一段时间更改一次......
但是,当我添加listview更新功能时,我得到一个stackoverflow异常。有没有人介意帮助初学者试图弄明白这件事?
此外,在stackoverflow异常抛出之前,我没有在listview中获得预期的结果;我希望看到更新文本。相反,所有列和行都会消失为白色。
以下是一些代码:
class FolderSelectionClass
{
private FormMain formMain;
public FolderSelectionClass(FormMain myFormMain)
{
formMain = myFormMain;
}
public void GetPictureCount(string selectedPath)
{
try
{
int count = 0;
//for each file in the path, is its file extension in our settings?
//If so, then add it to the count
foreach (ListViewItem listViewItem in formMain.ListView_Folders.Items)
{
if (selectedPath == listViewItem.SubItems[1].Text)//[1] = folder path
{
Stopwatch myStopwatch = new Stopwatch();
//If subdirectories incuded
if (listViewItem.Checked == true)
{
CountPicturesInPath(selectedPath, true, ref count, listViewItem.Index, myStopwatch);
}
else//not checked..subdirectories not included
{
CountPicturesInPath(selectedPath, false, ref count,listViewItem.Index, myStopwatch);
}
}
}
//update listview
formMain.ListView_Folders.BeginUpdate();
foreach (ListViewItem item in formMain.ListView_Folders.Items)
{
//find the item in the listview that is being updated
if (selectedPath == item.SubItems[1].Text)//[1] = folder path
{
item.SubItems[2].Text = count.ToString();
break;
}
}
formMain.ListView_Folders.EndUpdate();
formMain.ListView_Folders.Refresh();
}
catch (Exception e)
{
MessageBox.Show(e.Message + " (in GetPictureCount())");
}
}
private void CountPicturesInPath(string path,bool includeSubs, ref int pictureCount, int indexOfListViewItem, Stopwatch stopWatch)
{
try
{
//start listview processing animation
CheckTime(indexOfListViewItem,stopWatch);
foreach (string filePath in Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly))
{
try
{
FileInfo file = new FileInfo(filePath);
//for each item listed in file extension settings, check it against
//this file extension. If they match, add it to the count
foreach (string s in Properties.Settings.Default.FileExtensions)
{
if (s == file.Extension)
{
pictureCount++;
break;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + " (in CountPicturesInPath())");
}
}
//Now that we have counted the pictures in the current folder, if we are
//including subfolders, then we need to move to the next valid folder
//and count the pictures there.
if (includeSubs == true)
{
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
!= FileAttributes.ReparsePoint)
{
foreach (string folder in Directory.EnumerateDirectories(path))
{
CountPicturesInPath(folder, includeSubs, ref pictureCount, indexOfListViewItem, stopWatch);
}
}
}
}
catch (UnauthorizedAccessException) { }
}
private void CheckTime(int indexOfListViewItem, Stopwatch myStopwatch)
{
if (!myStopwatch.IsRunning)
{
myStopwatch.Start();
ProcessingPictureCount_Animation(indexOfListViewItem);
}
if (myStopwatch.ElapsedMilliseconds >= 2000)
{
ProcessingPictureCount_Animation(indexOfListViewItem);
myStopwatch.Reset();
}
}
private void ProcessingPictureCount_Animation(int indexOfListViewItem)
{
//the idea here is to add a Processing... animation in the count column
//while the count is going on, so that the user knows that the program is
//still working.
ListViewItem item = formMain.ListView_Folders.Items[indexOfListViewItem];
string myText = item.SubItems[2].Text;
if (myText == "0")
{
item.SubItems[2].Text = "Processing.";
}
else if (myText == "Processing.")
{
item.SubItems[2].Text = "Processing..";
}
else if (myText == "Processing..")
{
item.SubItems[2].Text = "Processing...";
}
else if (myText == "Processing...")
{
item.SubItems[2].Text = "Processing....";
}
else if (myText == "Processing....")
{
item.SubItems[2].Text = "Processing.....";
}
else if (myText == "Processing.....")
{
item.SubItems[2].Text = "Processing.";
}
formMain.ListView_Folders.BeginUpdate();
formMain.ListView_Folders.Items[indexOfListViewItem] = item;
formMain.ListView_Folders.EndUpdate();
formMain.ListView_Folders.Refresh();
}
}