字符串在MS Word文档中并行搜索而不打开它

时间:2016-07-03 15:30:31

标签: c# .net ms-word

我正在进行并行搜索,搜索所有Word文档(.doc)中的字符串,而不打开它,然后列出列表框中的所有返回文件。
我在文件夹中有大量文件(1000个文档) 仅使用foreach时,需要很长时间,所以我正在尝试parallel.foreach

使用Parallel.Foreach时,它只会将3个文件返回到列表框中,然后停止搜索!

CODE

var files = Directory.EnumerateFiles(p1, "*.doc",    SearchOption.AllDirectories).Where(file => (File.GetAttributes(file) & FileAttributes.Hidden) == 0).ToArray();

Parallel.ForEach(files, (f, loopState) =>
{
    qefl = WaitForm1.qafl;
    if (f.Trim().StartsWith("~"))
    {
        loopState.Stop();
    }
    if (qefl == 1)
    {
        loopState.Stop();
    }
    try
    {
        string str = Path.GetFileNameWithoutExtension(f);
        string pt = Path.GetFullPath(f);
        label2.Text = pt;
        wordApp.Visible = false;
        wordfile = wordApp.Documents.Open(pt, ReadOnly: true, PasswordDocument: "password", Visible: false);
        int docc = wordfile.Paragraphs.Count;
        Range rng = wordfile.Paragraphs[docc].Range;
        rng.Find.ClearFormatting();
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = false;
        object replace = 2;
        object wrap = 1;
        if (rng.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
        {
            FileSearch sh = new FileSearch();
            sh.FullPath = pt;
            sh.FileName = str;
            list.Add(sh);
        }
        rng.Select();
        wordfile.Close(false, Type.Missing, Type.Missing);
       System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordfile);
    }
    catch (Exception ex)
    {
        //    MessageBox.Show(ex.Message);
    }
});

listBoxControl4.DataSource = list;
listBoxControl4.DisplayMember = "FileName";
listBoxControl4.ValueMember = "FullPath";

问题

  1. 问题出在哪里?
  2. 有没有办法让搜索更快?

0 个答案:

没有答案