0

特定のディレクトリから多くのテキスト ファイルを検索し、次に textchanged イベントを検索して、すべてのファイル内のテキストを検索し、そのテキストを含む行のみを画面に表示しようとしています。

現在、動作していますが、遅すぎます。テキストを検索してリストボックスに表示する機能を投稿しています。少し速く動作させるための最も効率的な方法は何ですか。

listBox2.Items.Clear();
ArrayList lines = new ArrayList();

if (txtfile.Count > 0)
{
    for (int i = 0; i < txtfile.Count; i++)
    {
        lines.AddRange((File.ReadAllLines(Path.Combine(path, txtfile[i].ToString()))));
    }
    for (int i = 0; i < lines.Count; i++)
    {
        if(lines[i].ToString().IndexOf(txt,StringComparison.InvariantCultureIgnoreCase)>=0)

        {
                listBox2.Items.Add(lines[i].ToString());
        }       
    }

}
4

2 に答える 2

2

いくつのファイルを検索していますか? いつでもインデックスを作成し、コンテンツを SQL データベースに保存し、もちろん Parallel.For を使用できます。

Parallel.For(1, 1000, i =>
    {
        //do something here.
    }
);
于 2013-04-03T09:07:25.983 に答える
0

I would use Directory.EnumerateFiles and File.ReadLines since they are less memory hungry:

var matchingLines = Directory.EnumerateFiles(path, ".txt", SearchOption.TopDirectoryOnly)
    .SelectMany(fn => File.ReadLines(fn))
    .Where(l => l.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) >= 0);
foreach (var line in matchingLines)
    listBox2.Items.Add(line);

I would also search only when the user triggers it explicitely, so on button-click and not on text-changed.

于 2013-04-03T09:15:42.937 に答える