我正在构建播放 Wireshark 文件并使用 pcapdot.net 将数据包发送到网卡的应用程序。
在我的应用程序中,我获取目录根目录并仅添加过去 24 小时内的新文件。首先,我将目录中的所有文件添加到 List,然后添加到 ListView。现在,在我的应用程序完成播放我的所有文件后,我想清除我的列表,清除我的 ListView 并再次搜索过去 24 小时内的 now 文件并添加到我的 ListView。我的问题是,如果我选择包含 2 个文件的文件夹,添加 2 个文件,并且在应用程序完成播放这 2 个文件之后,我List<string>
现在ListView
是空的,应用程序现在不仅再次添加这 2 个文件,而且多次添加这个文件我找不到问题所在。
List<string> capturesList = null;
string pathToSearch = null;
DialogResult dialog;
lvFiles is my ListView
添加目录按钮
private void btnAddDir_Click(object sender, EventArgs e)
{
string fileToAdd = string.Empty;
capturesList = new List<string>();
dialog = folderBrowserDialog1.ShowDialog();
tbAddDir.Text = folderBrowserDialog1.SelectedPath;
pathToSearch = folderBrowserDialog1.SelectedPath;
if (dialog == DialogResult.OK)
{
toolStripStatusLabel.Text = "Search for pcap files...";
groupBoxRootDirectory.Enabled = false;
btnStart.Enabled = false;
addFiles();
}
}
和添加文件:
private void addFiles()
{
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(
(s3, e3) =>
{
capturesList = SafeFileEnumerator.EnumerateFiles(pathToSearch, "*.pcap",
SearchOption.AllDirectories).ToList();
});
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
if (capturesList.Count != 0)
AddFilesToListView(capturesList);
});
backgroundWorker.RunWorkerAsync();
}
将文件添加到我的 ListView:
private void addFileToListBox(string filePath, string duration)
{
ListViewItem item = new ListViewItem(new string[] { new FileInfo(filePath).Name, duration, "Waiting" });
item.Tag = new FileInfo(filePath).FullName;
this.Invoke((MethodInvoker)delegate { lvFiles.Items.Add(item); });
}
private void AddFilesToListView(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) =>
{
for (int i = 0; i < filesList.Count; i++)
{
FileInfo fileInfo = new FileInfo(filesList[i]);
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)
{
addFileToListBox(fileToAdd, capinfos.getFileDuration(fileToAdd));
}
}
}
}
}
};
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s1, e1) =>
{
});
backgroundWorker.RunWorkerAsync();
}
开始播放按钮:
private void btnStart_Click(object sender, EventArgs e)
{
playFiles();
}
playFiles function:
private void playFiles()
{
lockButtons();
string filePath = string.Empty;
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(
(s3, e3) =>
{
for (int i = 0; i < lvFiles.Items.Count && shouldContinue; i++)
{
PcapFile pcapFile = new PcapFile();
pcapFile.sendQueue(filePath, adapter);
}
});
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
capturesList.RemoveRange(0, capturesList.Count);
lvFiles.Items.Clear();
addFiles();
playFiles();
});
backgroundWorker.RunWorkerAsync();
}
在我完成 backgroundWorker.RunWorkerCompleted 中的 playFiles 函数后,我清除了所有列表并再次从我的目录根目录添加文件并再次 playFiles:
capturesList.RemoveRange(0, capturesList.Count);
lvFiles.Items.Clear();
addFiles();
playFiles();