我正在使用SafeFileEnumerator
类来搜索文件夹根目录中的文件,该类仅从具有权限的位置返回文件,因此我成功使用了它。
当我尝试以这种方式填写我的列表时,一切正常:
List<string> list = new List<string>();
list = SafeFileEnumerator.EnumerateFiles(folderBrowserDialog1.SelectedPath,
"*.*", SearchOption.AllDirectories).ToList();
但在这种情况下,我的 GUI 冻结了,所以我将它移动到新线程,在这种情况下什么也没有发生,我想知道为什么:
private void btnAddDir_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
string fileToAdd = string.Empty;
List<string> filesList = new List<string>();
dialog = folderBrowserDialog1.ShowDialog();
tbAddDir.Text = folderBrowserDialog1.SelectedPath;
string pathToSearch = folderBrowserDialog1.SelectedPath;
if (dialog == DialogResult.OK)
{
toolStripStatusLabel.Text = "Search for pcap files...";
groupBoxRootDirectory.Enabled = false;
btnStart.Enabled = false;
ThreadStart starter = delegate
{
list = SafeFileEnumerator.EnumerateFiles(pathToSearch, "*.pcap", SearchOption.AllDirectories).ToList();
};
Thread t = new Thread(starter);
t.IsBackground = true;
t.Start();
if(list.Count != 0)
AddFilesToListBox(list);
}
}
SafeFileEnumerator 类代码:
public static class SafeFileEnumerator
{
public static IEnumerable<string> EnumerateDirectories(string parentDirectory, string searchPattern, SearchOption searchOpt)
{
try
{
var directories = Enumerable.Empty<string>();
if (searchOpt == SearchOption.AllDirectories)
{
directories = Directory.EnumerateDirectories(parentDirectory).SelectMany(x => EnumerateDirectories(x, searchPattern, searchOpt));
}
return directories.Concat(Directory.EnumerateDirectories(parentDirectory, searchPattern));
}
catch (UnauthorizedAccessException ex)
{
return Enumerable.Empty<string>();
}
}
public static IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOpt)
{
try
{
var dirFiles = Enumerable.Empty<string>();
if (searchOpt == SearchOption.AllDirectories)
{
dirFiles = Directory.EnumerateDirectories(path).SelectMany(x => EnumerateFiles(x, searchPattern, searchOpt));
}
return dirFiles.Concat(Directory.EnumerateFiles(path, searchPattern));
}
catch (UnauthorizedAccessException ex)
{
return Enumerable.Empty<string>();
}
}
}
在我的列表充满文件之后:
private void AddFilesToListBox(List<string> filesList)
{
string fileToAdd = string.Empty;
Editcap editcap = new Editcap();
Capinfos capinfos = new Capinfos();
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork +=
(s1, e1) =>
{
foreach (string fileName in filesList)
{
FileInfo fileInfo = new FileInfo(fileName);
if (checkFileCreationDate(fileInfo))
{
if (editcap.isWiresharkFormat(fileInfo.FullName))
{
if (editcap.isLibpcapFormat(fileInfo.FullName))
{
addFileToListBox(fileInfo.FullName, capinfos.getFileDuration(fileInfo.FullName));
}
else if (!editcap.isLibpcapFormat(fileInfo.FullName))
{
fileToAdd = editcap.getNewFileName(fileInfo.FullName);
if (new FileInfo(fileToAdd).Exists && !fileInfo.Exists)
{
addFileToListBox(fileToAdd, capinfos.getFileDuration(fileInfo.FullName));
}
}
}
}
//this.Invoke((MethodInvoker)delegate { lvFiles.Items[lvFiles.Items.Count - 1].Selected = true; });
toolStripStatusLabel.Text = string.Format("{0} files were added", lvFiles.Items.Count);
}
//this.Invoke((MethodInvoker)delegate { lvFiles.Items[lvFiles.Items.Count - 1].Selected = true; });
};
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s1, e1) =>
{
groupBoxRootDirectory.Enabled = true;
btnClear.Enabled = true;
if (lvFiles.Items.Count != 0)
btnStart.Enabled = true;
toolStripStatusLabel.Text = string.Format("{0} files were added", lvFiles.Items.Count);
if (tbAddDir.Text != "")
{
listener = new FileListener();
listener.startListener(tbAddDir.Text);
listener._newFileEventHandler += listener_newFileEventHandler;
}
});
backgroundWorker.RunWorkerAsync();
}