【问题标题】:c# how to end streamreaderc#如何结束streamreader
【发布时间】:2017-02-17 00:35:41
【问题描述】:

我正在为 Uni 中的分配做一个项目 Windows 表单,我想搜索一个已经创建的文本文件以匹配名字和姓氏,然后如果名字和姓氏存在,则写一些附加信息。我已经构建了代码并且没有显示错误,但是当我运行并尝试添加信息时,我收到了一个错误,该错误基本上说明了下一个进程(Streamreader writer 无法访问该文件,因为它已被另一个进程使用)我假设这个过程是流式阅读器,我试图对其进行编码以停止阅读无济于事。我正在学习编码的前 3 个月,如果可能的话,我将不胜感激,我已将我的代码的 sn-p 放在下面。

//check if there is a file with that name
        if (File.Exists(sFile))
        {

            using (StreamReader sr = new StreamReader(sFile))
            {
                //while there is more data to read
                while (sr.Peek() != -1)
                {
                    //read first name and last name
                    sFirstName = sr.ReadLine();
                    sLastName = sr.ReadLine();
                }
                {
                    //does this name match?
                    if (sFirstName + sLastName == txtSearchName.Text)

                        sr.Close();
                }

                //Process write to file
                    using (StreamWriter sw = new StreamWriter(sFile, true))

                        {
                            sw.WriteLine("First Name:" + sFirstName);
                            sw.WriteLine("Last Name:" + sLastName);
                            sw.WriteLine("Gender:" + sGender);
                        }

【问题讨论】:

  • StreamReader 的右括号在哪里。 using 指令将处理流读取器并释放它对文件的锁定,但看起来好像直到打开编写器之后才关闭它?
  • 读取你的输入文件,写入一个临时文件,然后用你的临时文件粉碎你的输入文件。
  • 你确定这部分代码被执行了吗?因为您似乎正在尝试同时读取和写入同一个文件。 (似乎是个糟糕的主意)
  • 您是想将附加数据附加到同一文件的末尾还是名称所在行的末尾?

标签: c# streamreader


【解决方案1】:

您在阅读器中使用您的编写器,使用相同的文件。 using 将对象放置在其中,在右花括号之后。

 using(StreamReader reader = new StreamReader("foo")){
 //... some stuff
   using(Streamwriter writer = new StreamWriter("foo")){
   }
 }

这样做:

 using(StreamReader reader = new StreamReader("foo")){
 //... some stuff 
 }

 using(Streamwriter writer = new StreamWriter("foo")){
 }

【讨论】:

    【解决方案2】:

    根据我对 using 语句的评论。 重新排列如下。我已经在本地测试过,它似乎可以工作。

       using (StreamReader sr = new StreamReader(sfile))
       {
            //while there is more data to read
            while (sr.Peek() != -1)
            {
                //read first name and last name
               sFirstName = sr.ReadLine();
               sLastName = sr.ReadLine();
    
                 //does this name match?
                if (sFirstName + sLastName == txtSearchName.Text)
                    break;
            }
         }
    
        using (StreamWriter sw = new StreamWriter(sfile, true))
        {
            sw.WriteLine("First Name:" + sFirstName);
            sw.WriteLine("Last Name:" + sLastName);
            sw.WriteLine("Gender:" + sGender);
        }
    

    我已将 sr.Close 替换为 break 语句以退出。关闭阅读器会导致随后的 peek 在它关闭时出错。

    另外,我注意到您没有设置性别?除非它在其他地方设置。 希望有帮助

    【讨论】:

    • 嗨,伙计们,非常感谢您的帮助,我已经删除了性别,因为不再需要它,如果我收到与中断相关的错误,我在使用此代码时仍然存在问题; “没有可以中断或继续的封闭循环”??
    • 奇怪.. 仍然对我有用.. 你确定你没有休息吗? while 括号外的语句 { } ?
    【解决方案3】:

    您可以使用 FileStream。它为您提供了许多使用文件的选项:

    var fileStream = new FileStream("FileName", FileMode.Open,
     FileAccess.Write, FileShare.ReadWrite);
    
    
    var fileStream = new FileStream("fileName", FileMode.Open, 
    FileAccess.ReadWrite, FileShare.ReadWrite);
    

    【讨论】:

      【解决方案4】:

      我认为这是您想要/需要的。您无法按照尝试的方式附加到文件。相反,您需要读取输入文件,并在阅读时编写一个临时文件。而且,只要您的行符合您的要求,您就可以编写带有修改的行。

              string inputFile = "C:\\temp\\StreamWriterSample.txt";
              string tempFile = "C:\\temp\\StreamWriterSampleTemp.txt";
      
              using (StreamWriter sw = new StreamWriter(tempFile))//get a writer ready
              {
                  using (StreamReader sr = new StreamReader(inputFile))//get a reader ready
                  {
                      string currentLine = string.Empty;
                      while ((currentLine = sr.ReadLine()) != null)
                      {
                          if (currentLine.Contains("Clients"))
                          {
                              sw.WriteLine(currentLine + " modified");
                          }
                          else
                          {
                              sw.WriteLine(currentLine);
                          }
                      }
                  }
              }
      
              //now lets crush the old file with the new file
              File.Copy(tempFile, inputFile, true);
      

      【讨论】:

      • 我有一个 Windows 窗体,它允许在写入文件的 6 行输入中输入客户详细信息。我有表格的另一部分包含在不同机器上执行的锻炼分钟的复选框,我有一个搜索框和一个按钮,我正在尝试在当前文件中搜索客户的名字和姓氏“输入的名称搜索框”,如果存在则附加新的分钟信息。有一个单选按钮可以选择活动进行的日期。所以需要附加文件或添加新行而不覆盖 .我需要进行哪些更改?
      • 您需要彻底重新考虑您的方法并使用数据库,或者至少使用 XML 文件。相比之下,像数据库一样读取和重写文本文件的性能会很差。不过,要使我的代码在您的场景中工作,您只需要摆弄 if 语句。但是你有 3 行要修改...要么运行 3 个单独元素的搜索,要么运行一次并假设如果找到名字,接下来的 2 行不可避免地是姓氏和性别。无论哪种情况,您最终都会在每次进行更新时重新编写您的数据库
      猜你喜欢
      • 2010-11-10
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多