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

时间:2011-10-07 06:23:52

标签: c# .net 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);
        }

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:3)

滚动您自己的CSV阅读器是浪费时间,除非您正在阅读的文件保证 非常简单。请改用pre-existing, tried-and-tested implementation