同步文件搜索 [英] A Synchronous File Search

查看:68
本文介绍了同步文件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想搜索C:\\中的所有Html(s)文件。

我正在使用这种方法:

Hi ,
I want to search all Html(s) File in C:\\ .
I am using this Method :

public List<string> SearchFilesRecursive(string directory, string pattern)
{
    Stack<string> stack = new Stack<string>();
    List<string> result = new List<string>();
    stack.Push(directory);
    List<string> current_dir = new List<string>();
    int extention_length = pattern.Length - 1;
    while (stack.Count > 0)
    {
        string dir = stack.Pop();
        try
        {
            current_dir.AddRange(Directory.GetFiles(dir, pattern));
            for (int i = 0; i < current_dir.Count; i++)
            {
                if (current_dir[i].LastIndexOf(".") != current_dir[i].Length - extention_length)
                {
                    current_dir.RemoveAt(i);
                    i--;
                }
            }
            result.AddRange(current_dir);


            current_dir.Clear();

            // Add all directories at this directory.
            foreach (string dn in Directory.GetDirectories(dir))
            {
                stack.Push(dn);
            }
        }
        catch
        {
            // Couldn't open the directory
        }
    }
    return result;
}



然后使用此代码进行搜索:


and then using this code for search :

List<string> htmlobj = SearchFilesRecursive(@"C:\", "*.html");
for (int i = 0; i < htmlobj.Count; i++)
    listBox1.Items.Add(htmlobj[i]);



它运行良好且真实但它不是同步的,这意味着当.html文件发现程序不同时添加此文件到列表框!

我知道,我必须在方法期间将文件添加到列表框但是在哪里?!



任何人都可以解释我,我该如何解决它?! (我在哪里将它添加到此方法的列表框中?!)



谢谢你。


It works good and true but It is not Synchronous , It means when a .html file found program dont add this file Synchronously to listbox !
I know , I must add file to listbox during the method but where ?!

Can anybody explain me , how can I solve it ?! (where do I add it to listbox in this method ?!)

thank u .

推荐答案

嗯。

您是否考虑过:

Um.
Have you considered:
List<string> files = Directory.GetFiles(@"C:\", "*.html", SearchOption.AllDirectories).ToList();
return files;


您必须将对列表框的引用传递给您方法然后将项添加到列表框。请记住,您的方法必须在另一个线程中执行,如果您使用的是wpf,则必须使用Dispatcher.Invoke才能编辑列表框。 I.E.你必须在调用listbox.items.add方法的dispatcher.invoke中使用delegate / lambda表达式。

如果你使用winform,请使用backgroundworker。

如果您不想在方法中传递列表框,则可以声明observablecollection< string>并使用async / await模式运行您的方法(或者在另一个线程中,意识到您的ObservableCollection必须是volatile,并且在任何情况下,不在方法内部,而是作为全局变量)。

然后,在调用方法之前,必须将列表框的itemssource设置为ObservableCollection。这个方法只适用于wpf,我用winform知道的唯一方法就是后台工作。



希望它有所帮助。



Jymmy097
You have to pass a reference to the listbox to your method then add items to the listbox. Remember that your method has to be executed in another thread and that, if you are using wpf, you have to use Dispatcher.Invoke in order to be able to edit the listbox. I.E. you have to use a delegate/lambda expression inside the dispatcher.invoke which calls the listbox.items.add method.
if you are using winform, use a backgroundworker.
if you don't want to pass your listbox inside the method, you can declare an observablecollection<string> and run your method with the async/await pattern (or in another thread, being conscious that your ObservableCollection has to be volatile and, in any case, not inside the method, but as a global variable).
Then, before calling your method, you have to set the itemssource of your listbox to your ObservableCollection. This method works only on wpf, the only method I know with winform is the backgroundworker.

hope it helps.

Jymmy097


您应该将 SearchFilesRecursive 方法的返回类型定义为 IEnumerable< string>

并使用收益率 [ ^ ]退货声明(需要您进行一些重构) 。



然后你必须修改你的搜索代码:

You should define the return type of the SearchFilesRecursive method as IEnumerable<string>.
And use the yield[^] statement for returns (that will need some refactoring from you).

Then you would have to modify your search-code:
foreach (string s in htmlobj) {
   listBox1.Items.Add(s);
}





但是,如果你的搜索方法在主线程上运行,你几乎没有机会看到列表框的任何更新。你必须在另一个线程上执行搜索。

BackgroundWorker类 [ ^ ]是一个很好的,很容易实现的方法。



但是,最重要的是,我发现你的搜索算法也是如此你的列表和堆栈非常复杂;从技术上讲,你唯一需要的就是清单。你应该重新考虑它的设计。

此外,你的算法不是严格递归的,因为它实际上从不调用自己。



留下来退出以递归的方式,您的要求可以简化为:



But then, if your search-method runs on the main thread, there are little chances that you will see any update of the listbox. You have to execute the search on another thread.
BackgroundWorker Class[^] is a good and rather easy way to achieve that.

But, most important, I find your search algorithm too much complex with your list and stack; technically the only thing you need is a list. You should reconsider its design.
Moreover, your algorithm is not strictly recursive, as it never actually calls itself.

Left to quit the recursive way, your requirement could be simplified to:

public IEnumerable<string> SearchFiles(string directory, string pattern) {
   foreach (string s in Directory.GetFiles(dir, pattern, SearchOption.AllDirectories)) {
      yield return s;
   }
}


这篇关于同步文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆