c# - 无法在多个位置添加或插入项目 MyFile

标签 c# winforms

我正在构建播放 Wireshark 文件并使用 pcapdot.net 将数据包发送到网卡的应用程序。

在我的应用程序中,我获取根目录并仅添加过去 24 小时内的新文件。 首先,我将目录中的所有文件添加到 List<string>然后到我的ListView . 现在,在我的应用程序播放完我的所有文件后,我想清除我的列表,清除我的 ListView,然后再次搜索最近 24 小时内的文件并添加到我的 ListView . 我的问题是在将文件再次添加到我的列表并尝试将文件添加到我的 ListView 之后应用程序崩溃并出现错误 无法在多个位置添加或插入项目“file.pcap”。您必须先将其从当前位置移除或克隆。

完成播放我的文件后,我清理列表、ListView 并再次添加文件并播放:

            lvFiles.Items.Clear();
            filesList.RemoveRange(0, filesList.Count - 1);
            addFiles();
            playFiles();

添加文件(); - 将 List 中的所有文件添加到我的 ListView:

private void addFiles()
{
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork += new DoWorkEventHandler(
    (s3, e3) =>
    {
        filesList = SafeFileEnumerator.EnumerateFiles(pathToSearch, "*.pcap",
            SearchOption.AllDirectories).ToList();
    });

    backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {
            if (filesList.Count != 0)
                AddFilesToListView(filesList);
        });

    backgroundWorker.RunWorkerAsync();
}

playFiles() - 在我的 ListView 中播放我的所有文件:

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++)
        {
            this.Invoke((MethodInvoker)delegate
            {
                lvFiles.EnsureVisible(i);
                lvFiles.Items[i].Selected = true;
                lvFiles.Select();
                filePath = lvFiles.Items[i].Tag.ToString();
                toolStripStatusLabel.Text = string.Format("Current file: {0}", lvFiles.Items[i].Text);
            });

            PcapFile pcapFile = new PcapFile();
            pcapFile.sendQueue(filePath, adapter);
            this.Invoke((MethodInvoker)delegate { lvFiles.Items[i].Selected = false; });
        }
    });

将文件添加到我的 ListView:

private void addFileToListBox(string filePath, string duration)
{
    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); });
}

也许“某人”仍然持有该文件?

最佳答案

我很确定这行可能是您的问题:

item = new ListViewItem(new string[] { new FileInfo(filePath).Name, duration, "Waiting" });

这会创建一个新对象,但仍然引用 item,从外观上看,它声明的范围比您预期的要大,可能是整个表单。

您需要使对象的范围成为您将其添加到 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); });
        }

如果您在更大的范围内声明了您的变量 item,那么每次您创建一个新的 ListViewItem 时,您实际上仍然在 ListView 中使用与第一次相同的项目。

虽然您没有显示声明 item 的位置,但您还需要删除在错误范围内声明它的行:

ListViewItem item; // <-- Delete me

关于c# - 无法在多个位置添加或插入项目 MyFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244587/

相关文章:

c# - 获取表单实例的函数

c# - 将 XML 加载到内存流中

c# - 从后面的代码更改 JS 代码

c# - 检查对象是否为空或 null

c# - 删除选项卡上的按钮边框 c# winforms

c# - 为什么在单击向下箭头之前,domainupdown 控件不会更改值?

c# - 解析动态枚举

c# - JSON反序列化对象不起作用

c# - 更改 WinForm 控件的字体

c# - bool 逻辑/真值表和输出