同时使用两个流阅读器来比较和替换列和行?

Use two streamreaders at the same time for comparing and replacing columns and rows?

看,我正在编写一个简单的程序来替换 CSV 文件中的数据。

我需要替换的是第 2 列(parent)来设置 ID 的名称而不是 ID,像这样:

原文件:

ID    - PARENT - NAME
ID 1  -        - Name of ID 1
ID 2  -  ID 1  - Name of ID 2
ID 3  -  ID 2  - Name of ID 3
ID 4  -  ID 1  - Name of ID 4
ID 5  -  ID 4  - Name of ID 5

结果:

ID    - PARENT         - NAME
ID 1  -                - Name of ID 1
ID 2  -  Name of ID 1  - Name of ID 2
ID 3  -  Name of ID 2  - Name of ID 3
ID 4  -  Name of ID 1  - Name of ID 4
ID 5  -  Name of ID 4  - Name of ID 5

所以我需要在这里做一个递归循环,使用同一个 StreamReader 的两个实例来比较每一个。我不是在问如何进行循环,而是在问如何同时使用两个流阅读器,因为我试图通过创建所有必要的变量来做到这一点,但 C# 似乎不接受它,它就是不接受工作,数据全部变为空白......并且也没有给出任何错误。我该怎么做?

有效的部分:

filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
{
    fileContent = reader.ReadLine();
    int i = 1;
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
   {
      while (fileContent != null)
          {
              /// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
          }
   }
writer.Flush();
writer.Close();
}

我试过的方法不起作用:

System.IO.StreamReader reader2 = reader; // create a copy of the reader object...

我想访问同一个文件的两个独立的流读取器,但它不能像这样工作...

您的代码存在的问题是 C# 中的 类 是按引用传递的,而不是按值传递的。

这意味着这一行:

System.IO.StreamReader reader2 = reader;

...不是 如您所料创建新的 StreamReader,而是使用相同的流创建了对相同 StreamReader 的另一个引用位置。

您需要做的是创建一个新的 StreamReader 和一个新的 FileStream,如下所示:

filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
var secondFileStream = openFileDialog.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
using (System.IO.StreamReader reader2 = new System.IO.StreamReader(secondFileStream, Encoding.GetEncoding("iso-8859-1")))
{
    fileContent = reader.ReadLine();
    int i = 1;
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
    {
        while (fileContent != null)
        {
            /// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
        }
    }
    writer.Flush();
    writer.Close();
}

我不确定 运行 OpenFileDialog.OpenFile() 两次的功能,所以如果这不起作用,试试这个:

filePath = openFileDialog.FileName;
using (FileStream fileStream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (FileStream secondFileStream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
using (System.IO.StreamReader reader2 = new System.IO.StreamReader(secondFileStream, Encoding.GetEncoding("iso-8859-1")))
{
    fileContent = reader.ReadLine();
    int i = 1;
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
    {
        while (fileContent != null)
        {
            /// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
        }
    }
    writer.Flush();
    writer.Close();
}