使用C#加速Windows文件搜索

时间:2009-11-15 19:00:20

标签: c# .net windows search

我制作了一个程序,搜索逻辑驱动器以查找特定文件.if用户类型文件名 点击搜索按钮,搜索开始,但我不知道如何在进程中停止搜索。你帮我吗?

4 个答案:

答案 0 :(得分:4)

您应该在后台线程上执行搜索,以便它不会阻止UI。 This article对您需要对您的应用进行的更改进行了很好的介绍和演练。

答案 1 :(得分:1)

您需要在后台线程中运行搜索(使用BackgroundWorker是最方便的方法)然后您仍然可以处理输入以取消它。

当您处理取消请求时,您可能需要在执行线程上使用Thread.Abort或在BackgroundWorker上使用BackgroundWorker.CancelAsync()。

或者,您可以让执行线程在处理循环或递归函数开始时检查变量 - 取消您在处理取消请求时设置此变量的简单需要。

答案 2 :(得分:1)

您需要在.net中使用Backgroundworker类。它在单独的线程上执行,它具有内置的方法/属性,用于取消,报告进度和更多...

请看下面的文章开始使用它:

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

答案 3 :(得分:0)

正如其他人所提到的,您的解决方案可能是BackgroundWorker使用CancelAsync方法。

以下是一些可以使用的经过修改的工作代码:

class Program
{
    static void Main(string[] args)
    {
        var search = new FileSearcher().FindFile(@"d:\users", "autofac.dll", f => Console.WriteLine(f.FullName), () => Console.WriteLine("Finished"));

        Console.WriteLine("C - cancel, else - finish");

        for (; ; )
        {
            var command = Console.ReadLine();
            switch (command)
            {
                case "C":
                    search.Cancel();
                    break;

                default:
                    return;
            }
        }
    }
}

public class FileSearcher
{
    public FileSearch FindFile(string searchPath, string fileName, Action<FileInfo> onFileFound, Action onSearchFinished)
    {
        var search = new FileSearch(new DirectoryInfo(searchPath), fileName);
        search.FileFound += onFileFound;
        search.Finished += onSearchFinished;
        search.Run();
        return search;
    }
}

public class FileSearch
{
    readonly BackgroundWorker _worker = new BackgroundWorker();
    readonly DirectoryInfo _searchPath;
    readonly string _template;

    public FileSearch(DirectoryInfo searchPath, string template)
    {
        _searchPath = searchPath;
        _template = template;

        _worker.DoWork += _worker_DoWork;
        _worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
        _worker.WorkerSupportsCancellation = true;
    }

    void _worker_DoWork(object sender, DoWorkEventArgs e)
    {
        foreach (var directory in GetPartiallyFlatDirectories(_searchPath, 4))
        {
            if (_worker.CancellationPending)
                break;

            foreach (var file in directory.GetFiles(_template, SearchOption.AllDirectories))
                FileFound.Raise(file);
        }
    }

    static IEnumerable<DirectoryInfo> GetPartiallyFlatDirectories(DirectoryInfo directory, int flatDepth)
    {
        if (flatDepth == 0)
        {
            yield return directory;
            yield break;
        }

        foreach (var subDir in directory.GetDirectories())
        {
            var flattened = GetPartiallyFlatDirectories(subDir, flatDepth - 1);

            if (!flattened.Any())
                yield return subDir;
            else
                foreach (var flatDirectory in flattened)
                    yield return flatDirectory;
        }
    }

    void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Finished.Raise();
    }

    public void Cancel()
    {
        _worker.CancelAsync();
    }

    public event Action<FileInfo> FileFound;
    public event Action Finished;

    public void Run()
    {
        _worker.RunWorkerAsync();
    }
}

public static class DelegateExtensions
{
    public static void Raise<T>(this Action<T> action, T obj)
    {
        if (action != null)
            action(obj);
    }

    public static void Raise(this Action action)
    {
        if (action != null)
            action();
    }
}