0

在我的应用程序中,我想在选择根文件夹后添加添加 Wireshark 文件的选项。

我正在使用此类从根文件夹(我有权访问的所有文件夹)中获取所有文件:

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 btnAddDir_Click_1(object sender, EventArgs e)
{
    string fileToAdd = string.Empty;
    DialogResult dialog = folderBrowserDialog1.ShowDialog();
    if (dialog == DialogResult.OK)
    {
        Editcap editcap = new Editcap();

        foreach (string fileName in SafeFileEnumerator.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories))
        {
            if (editcap.isWiresharkFormat(fileName))
            {
                if (editcap.isLibpcapFormat(fileName))
                {
                    listBoxFiles.Items.Add(fileName);
                }
                else if (!editcap.isLibpcapFormat(fileName))
                {
                    listBoxFiles.Items.Add(editcap.getNewFileName(fileName));
                }
            }
        }
    }
}

Editcap editcap = new Editcap();- 我的类使用 2 种方法检查文件是否是有效的 Wireshark 文件:isWiresharkFormatisLibpcapFormat. getNewFileName- 如果文件不是 pcap 文件,则将其转换为 pcap 文件。

我想要的是在不同的线程中添加所有正确的文件(Wireshark 文件)添加到我的列表框中,这样 GUI 就不会被阻塞。理想情况下,在找到每个文件后,它会立即添加到我的列表框中,而不是等到所有文件都被评估后。

4

1 回答 1

1

这是一种使用Reactive Extensions.

首先将对 RX 库的引用和以下using语句添加到您的类定义中:

using System.Reactive.Linq;
using System.Reactive.Concurrency;

然后,添加一个私有字段,当您关闭应用程序并修改方法时,该IDisposable字段将用于清理任何剩余的线程工作:btnAddDir_Click_1

IDisposable _disposable; 

private void btnAddDir_Click_1(object sender, EventArgs e)
{

    string fileToAdd = string.Empty;
    DialogResult dialog = folderBrowserDialog1.ShowDialog();
    if (dialog == DialogResult.OK)
    {
        Editcap editcap = new Editcap();

        var observable = SafeFileEnumerator.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories)
            .ToObservable()
            .SubscribeOn(NewThreadScheduler.Default);

        _disposable = observable.Subscribe((fileName) =>
            {
                if (editcap.isWiresharkFormat(fileName))
                {
                    if (editcap.isLibpcapFormat(fileName))
                    {
                        listBoxFiles.Items.Add(fileName);
                    }
                    else if (!editcap.isLibpcapFormat(fileName))
                    {
                        listBoxFiles.Items.Add(editcap.getNewFileName(fileName));
                    }
                }
            });
    }
}

YourForm.Designer.cs中,正确处理_disposable对象:

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }

    //dispose the observable IDisposable
    if (_disposable != null)
        _disposable.Dispose();

    base.Dispose(disposing);
}
于 2012-12-20T01:04:31.500 回答