C#linq Files.ReadAllLines()因大型650MB CSV文件而失败

Sai*_*man 0 .net c# linq

当我使用1MB以下的CSV文件时,以下代码有效,但当我尝试读取600MB文件时失败.有什么理由吗?或任何修复?

我想要做的是在Visual C#2010中读取一个大的原始CSV文件并操作内容,可以一行一行或一次性地存储,并使用LINQ导出5个具有特定选择的文件.这5个文件将用于各种过程,因此需要将它们分成5个不同的文件,内容非常不同.

当文件很小时,代码工作得很好但是当它太大时,它会从异常处理"无法写入源目标"中获取消息框.我试过ReadAllLines()和ReadLines()请你告诉我.谢谢.

public void button2_Click(object sender, EventArgs e)
    {

        string file_name = textBox1.Text.ToString();
        // Get the directories to split the file in.
        string directoryPath = Path.GetDirectoryName(textBox1.Text.ToString());
        if (File.Exists(file_name) == true)
        {
            try
            {
                StreamReader readerfile = new StreamReader(file_name);

                var BillSummaryQuery1 =
                    (from line in File.ReadAllLines(file_name)
                     let linerecord = line.Split(',').ToList()
                     select line).ToList();

                #region Start Writing BillSummary.CSV


                //lines removed

                #endregion End writing BillSummary.CSV

                #region Start writing Notes.CSV

                //lines removed


                #endregion Notes.CSV



                string message =
                        "Bill Translated Successfully! \r\nFiles located in: " + directoryPath;
                MessageBox.Show(message, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch (Exception)
            {
                string message2 = "Cannot write to source destination";
                MessageBox.Show(message2, "Error");



            }


        }
        else
        {
            MessageBox.Show("No such file exists","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
Run Code Online (Sandbox Code Playgroud)

Arn*_* F. 6

如果您使用的是StreamReader,为什么不使用它?

public void button2_Click(object sender, EventArgs e)
{
    string file_name = textBox1.Text.ToString();
    // Get the directories to split the file in.
    string directoryPath = Path.GetDirectoryName(textBox1.Text.ToString());
    if (File.Exists(file_name) == true)
    {
        try
        {
            using (StreamReader reader= new StreamReader(file_name))
            {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    // Do your stuff
                }
            }
        }

        catch (Exception ex)
        {
            Trace.TraceError(ex.Message);
            string message2 = "Cannot write to source destination";
            MessageBox.Show(message2, "Error");
        }
    }
    else
    {
        MessageBox.Show("No such file exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
Run Code Online (Sandbox Code Playgroud)