简体   繁体   English

从共享驱动器加载大量文本文件以搜索世界。 C#

[英]Loading large amount of text files from a share drive to search for a world. C#

Im trying to perform a search in a share drive that contains at least 90,000 files. 我试图在包含至少90,000个文件的共享驱动器中执行搜索。

The code below is what I have: 下面的代码是我所拥有的:

  private void button2_Click(object sender, EventArgs e)
  {

        if (string.IsNullOrEmpty(textBox1.Text))
        {
            MessageBox.Show("No folder: Not listed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {

            List<string> allFiles = new List<string>();
            AddFilesnames(sourceFolder, allFiles);

            foreach (string fileName in allFiles)
            {
                string contents = File.ReadAllText(fileName);
                if (contents.Contains(searchword))
                {
                    listBox1.BeginUpdate();
                    listBox1.Items.Add(fileName);
                    listBox1.EndUpdate();
                    label4.Text = (Convert.ToInt32(label4.Text) + 1).ToString();
                }

            }

            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("no files");
            }
        }
    }

    public void listboxtofile()
    {
        DialogResult resDialog = dlgSaveFile.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            FileInfo fi = new FileInfo(dlgSaveFile.FileName);
            StreamWriter sw = fi.CreateText();
            foreach (string sItem in listBox1.Items)
            {
                sw.WriteLine(sItem);
            }
            sw.Close();
        }
    }

    public  void AddFilesnames(string sourceDir, List<string> allFiles)
    {
        string[] fileEntries = Directory.GetFiles(sourceDir);
        foreach (string fileName in fileEntries)
        { 
            DateTime from1 = dateTimePicker1.Value.Date;
            DateTime to1 = dateTimePicker2.Value.Date;
            DateTime creationTime = File.GetCreationTime(fileName);

            if (creationTime >= from1 && creationTime <= to1)
            {
                allFiles.Add(fileName);
            }               

        }

        //Recursion   
        string[] subdirectoryEntries = Directory.GetDirectories(sourceDir);
        foreach (string item in subdirectoryEntries)
        {
            // Avoid "reparse points"
            if ((File.GetAttributes(item) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
            {
                AddFileNamesToList(item, allFiles);
                label4.Text = (Convert.ToInt32(label4.Text) + 1).ToString();
            }
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        textBox1.Clear();
    }

    private void button5_Click(object sender, EventArgs e)
    {
        listboxtofile();
    }
  }
}

So far this is working with 3,000 files but I need to have access to a shared drive that contains 90,000 files and when I try to search in the share drive the windows form get frozen. 到目前为止,这可以处理3,000个文件,但是我需要访问包含90,000个文件的共享驱动器,并且当我尝试在共享驱动器中搜索时,Windows窗体将被冻结。 I will really apreciate any help from you all. 我真的很感谢大家的帮助。

My suggestion would be to do any file system work in a separate thread. 我的建议是在单独的线程中执行任何文件系统工作。 If you do the file searching in the same thread as your form, Windows has no chance to update the form. 如果您在与表单相同的线程中搜索文件,则Windows将没有机会更新表单。 You won't be able to update any of your form controls (like listBox1) directly from the new thread, but you can look into delegates and C# threading in general for ways to work around this. 您将无法直接从新线程更新任何表单控件(例如listBox1),但是您通常可以研究委托和C#线程以解决此问题。

Also, if you just need to search files for a specific word, have you looked into existing tools like grep? 另外,如果您只需要在文件中搜索特定单词,那么是否研究了grep等现有工具?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM