是否有其他方法可以在短时间内读取许多文本文件

时间:2013-10-01 02:11:40

标签: c#

我在阅读1-20kb的文本文件时遇到问题。该文件夹有大约400,000个文件。 我限制程序只读取每个文本文件的第一行,但它仍然很慢。在读取文件之前,程序将从我选择的文件夹中获取文件名,检查文件名以查看它是否是我想要的,然后读取第一行并检查它是否正确,最后将文件复制到某个地方我想。

class FileChoose
{
    public string chooseFolder()
    {
        FolderBrowserDialog Fld = new FolderBrowserDialog();
        Fld.ShowNewFolderButton = false;
        if (Fld.ShowDialog() == DialogResult.OK)
        {
            return Fld.SelectedPath;
        }
        return "";
    }
    public List<string> getFileName(string path)
    {
        string[] filePaths = Directory.GetFiles(@path, "*.log");
        List<string> listPath = new List<string>();
        foreach (var item in filePaths)
        {
            string[] itemSplit = item.Split('\\');
            string year = itemSplit[itemSplit.Length - 1].Substring(0, 4);
            string month = itemSplit[itemSplit.Length - 1].Substring(4, 2);
            if ((year == "2013") && (month == "08"))
            {
            //    string fileNamePDF = itemSplit[itemSplit.Length - 1];
                listPath.Add(item);
            }
        }
        return listPath;
    }
    public bool isDrawing(string drawing, string path)
    {
        string drawingRead = readLog(path);
        if (drawingRead == drawing)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public string readLog(string path)
    {
        StreamReader sr = new StreamReader(path);
        string line;
        line = sr.ReadLine();
        string checkDrawing = line.Substring(1, 8);
        return checkDrawing;
    }
}

主要课程

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }
  //  public string pathGlobal = "D:\\OMT\\OMT1";
  //  public string pathGlobal2 = "D:\\OMT\\OMT2";

    public string pathGlobal = "D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT2-1";
    public string pathGlobal2 = "D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT3";
    List<string> listPath = new List<string>();
    List<string> listPath2 = new List<string>();
    List<string> listFile = new List<string>();
    FileChoose.FileChoose folder = new FileChoose.FileChoose();
    public string folderPath;
   // public string folderPath;
    private void button1_Click(object sender, EventArgs e)
    {
        string folderPathIN = folder.chooseFolder();
        //label1.Text = folderPathIN;
        this.folderPath = folderPathIN;
    }

    private void button2_Click(object sender, EventArgs e)
    {
  //      listPath = folder.getFileName(folderPath);
        listPath = folder.getFileName(pathGlobal);
        foreach (var item in listPath)
        {
         //   string pathFile = folderPath+item;
            bool check = folder.isDrawing("96642678", item);
            if (check)
                copyFile(item);

        }
        listPath2 = folder.getFileName(pathGlobal2);
        foreach (var item in listPath2)
        {
            //   string pathFile = folderPath+item;
            bool check = folder.isDrawing("96642678", item);
            if (check)
                copyFileSeparate(item);

        }
        MessageBox.Show("Success", "Success");
        label1.Text = "Copied files are in D:\\OMT_NEW";
    }
    public void copyFileSeparate(string item)
    {
        string[] splitItem = item.Split('\\');
        string folderName = splitItem[splitItem.Length - 1].Substring(0, 8);
        try
        {
            bool isExists = System.IO.Directory.Exists("D:\\OMTSeparate");
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMTSeparate");
            isExists = System.IO.Directory.Exists("D:\\OMTSeparate\\"+folderName);
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMTSeparate\\"+folderName);
            File.Copy(item, "D:\\OMTSeparate\\"+folderName+"\\" + splitItem[splitItem.Length - 1]);
        }
        catch (Exception)
        {
        }
    }
    public void copyFile(string item)
    {
        string[] splitItem = item.Split('\\');
        try
        {
            bool isExists = System.IO.Directory.Exists("D:\\OMT_NEW");
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMT_NEW");
            File.Copy(item, "D:\\OMT_NEW\\" + splitItem[splitItem.Length - 1]);

        }
        catch(Exception)
        {
        }
    }
   // 
}

1 个答案:

答案 0 :(得分:3)

这是很多文件。

第一步使用System.IO.Directory.EnumerateFiles而不是GetFiles。然后让GetFilename使用yield return返回IEnumerable。这将使您不必为400,000个文件名分配空间。打开文件仍然需要花费很多时间。你可以打开&amp;读取,取决于你的处理器和磁盘子系统,你可以做多少以及它有多大帮助。在一个小得多的测试用例上运行一些测试并使用它来确定所需的大致时间,并确保放入某种进度指示器,这样你就可以了解事情的进展情况。创建记录您的进度的偶尔检查文件也可能是有用的,所以如果发生了某些事情,您不必从头重新开始。