C# 有没有其他方法可以在短时间内读取许多文本文件

C# 有没有其他方法可以在短时间内读取许多文本文件,c#,C#,我在读取每个1-20kb的文本文件时遇到问题。该文件夹有大约400000个文件。 我已经将程序限制为只读取每个文本文件的第一行,但仍然很慢。在读取文件之前,程序将从我选择的文件夹中获取文件名,检查文件名是否符合我的要求,然后读取第一行并检查其是否正确,最后将文件复制到我想要的位置 class FileChoose { public string chooseFolder() { FolderBrowserDialog Fld = new FolderBrowser

我在读取每个1-20kb的文本文件时遇到问题。该文件夹有大约400000个文件。 我已经将程序限制为只读取每个文本文件的第一行,但仍然很慢。在读取文件之前,程序将从我选择的文件夹中获取文件名,检查文件名是否符合我的要求,然后读取第一行并检查其是否正确,最后将文件复制到我想要的位置

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;
    }
}
class文件选择
{
公共字符串chooseFolder()
{
FolderBrowserDialog Fld=新建FolderBrowserDialog();
Fld.ShowNewFolderButton=false;
if(Fld.ShowDialog()==DialogResult.OK)
{
返回Fld.SelectedPath;
}
返回“”;
}
公共列表getFileName(字符串路径)
{
字符串[]filepath=Directory.GetFiles(@path,*.log”);
List listPath=新列表();
foreach(文件路径中的变量项)
{
字符串[]itemSplit=item.Split('\\');
字符串year=itemSplit[itemSplit.Length-1]。子字符串(0,4);
字符串month=itemSplit[itemSplit.Length-1]。子字符串(4,2);
如果((年=“2013”)&(月=“08”))
{
//字符串fileNamePDF=itemSplit[itemSplit.Length-1];
添加(项);
}
}
返回列表路径;
}
公共布尔isDrawing(字符串绘制、字符串路径)
{
字符串drawingRead=readLog(路径);
if(drawingRead==图纸)
{
返回true;
}
其他的
{
返回false;
}
}
公共字符串读取日志(字符串路径)
{
StreamReader sr=新的StreamReader(路径);
弦线;
line=sr.ReadLine();
字符串checkDrawing=line.Substring(1,8);
返回检查图纸;
}
}
主类

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:表单
{
公共表格1()
{
初始化组件();
}
//公共字符串pathGlobal=“D:\\OMT\\OMT1”;
//公共字符串pathGlobal2=“D:\\OMT\\OMT2”;
公共字符串pathGlobal=“D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT2-1”;
公共字符串pathGlobal2=“D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT3”;
List listPath=新列表();
List listPath2=新列表();
List listFile=新列表();
FileChoose.FileChoose folder=新建FileChoose.FileChoose();
公共字符串folderPath;
//公共字符串folderPath;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串folderPathIN=folder.chooseFolder();
//label1.Text=folderPathIN;
this.folderPath=folderPathIN;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
//listPath=folder.getFileName(folderPath);
listPath=folder.getFileName(pathGlobal);
foreach(列表路径中的变量项)
{
//字符串路径文件=文件夹路径+项目;
bool check=folder.isDrawing(“96642678”,项目);
如果(检查)
复印件(项目);
}
listPath2=文件夹.getFileName(pathGlobal2);
foreach(listPath2中的变量项)
{
//字符串路径文件=文件夹路径+项目;
bool check=folder.isDrawing(“96642678”,项目);
如果(检查)
复制文件分离(项目);
}
MessageBox.Show(“Success”、“Success”);
label1.Text=“复制的文件位于D:\\OMT\u NEW”中;
}
public void copyFileSeparate(字符串项)
{
字符串[]splitItem=item.Split('\\');
字符串folderName=splitItem[splitItem.Length-1]。子字符串(0,8);
尝试
{
bool isExists=System.IO.Directory.Exists(“D:\\OMTSeparate”);
如果(!isExists)
System.IO.Directory.CreateDirectory(“D:\\omtsepart”);
isExists=System.IO.Directory.Exists(“D:\\OMTSeparate\\”+folderName);
如果(!isExists)
System.IO.Directory.CreateDirectory(“D:\\omtsepart\\”+folderName);
文件.Copy(项目“D:\\OMTSeparate\\”+folderName+“\\”+splitItem[splitItem.Length-1]);
}
捕获(例外)
{
}
}
公共无效复制文件(字符串项)
{
字符串[]splitItem=item.Split('\\');
尝试
{
bool isExists=System.IO.Directory.Exists(“D:\\OMT\u NEW”);
如果(!isExists)
System.IO.Directory.CreateDirectory(“D:\\OMT\u NEW”);
文件.Copy(项目“D:\\OMT\u NEW\\”+splitItem[splitItem.Length-1]);
}
捕获(例外)
{
}
}
// 
}
文件太多了


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

如果您使用的是.NET 4+,我应该查看
DirectoryInfo.EnumerateFiles()
-它应该可以节省一些前期加载和创建大量文件名列表的时间,并减少所需的内存。您是否尝试过使用
asynch
文件读取?我认为IO是这方面最慢的。或者,您也可以尝试对主文件循环使用
parralel.Foreach
,尽管返回的结果可能并不显著。@Kami:在I/O上使用多个线程会降低