【问题标题】:How to write data to a text file without overwriting the current data如何在不覆盖当前数据的情况下将数据写入文本文件
【发布时间】:2011-07-27 21:00:45
【问题描述】:

我似乎无法弄清楚如何在不覆盖文件的情况下将数据写入文件。我知道我可以使用 File.appendtext 但我不确定如何将其插入我的语法中。这是我的代码:

TextWriter tsw = new StreamWriter(@"C:\Hello.txt");

//Writing text to the file.
tsw.WriteLine("Hello");

//Close the file.
tsw.Close();

我希望它每次运行程序时都写 Hello,而不是覆盖之前的文本文件。感谢您阅读本文。

【问题讨论】:

标签: c# text overwrite


【解决方案1】:

通过trueas the append parameter of the constructor:

TextWriter tsw = new StreamWriter(@"C:\Hello.txt", true);

【讨论】:

    【解决方案2】:

    更改您的构造函数以将 true 作为第二个参数传递。

    TextWriter tsw = new StreamWriter(@"C:\Hello.txt", true);
    

    【讨论】:

      【解决方案3】:

      您必须以 new StreamWriter(filename, true) 的身份打开,以便它附加到文件而不是覆盖。

      【讨论】:

        【解决方案4】:

        这是一段将值写入日志文件的代码。如果文件不存在,它会创建它,否则它只是附加到现有文件。您需要添加“使用 System.IO;”在代码的顶部,如果它不存在的话。

        string strLogText = "Some details you want to log.";
        
        // Create a writer and open the file:
        StreamWriter log;
        
        if (!File.Exists("logfile.txt"))
        {
          log = new StreamWriter("logfile.txt");
        }
        else
        {
          log = File.AppendText("logfile.txt");
        }
        
        // Write to the file:
        log.WriteLine(DateTime.Now);
        log.WriteLine(strLogText);
        log.WriteLine();
        
        // Close the stream:
        log.Close();
        

        【讨论】:

          【解决方案5】:

          最好的事情是

          File.AppendAllText("c:\\file.txt","Your Text");
          

          【讨论】:

            【解决方案6】:

            查看 File 类。

            你可以用

            创建一个streamwriter
            StreamWriter sw = File.Create(....) 
            

            您可以使用

            打开现有文件
            File.Open(...)
            

            您可以轻松地添加文本

            File.AppendAllText(...);
            

            【讨论】:

              【解决方案7】:

              首先检查文件名是否已经存在,如果是,则创建一个文件并同时关闭它,然后使用AppendAllText 附加您的文本。欲了解更多信息,请查看下面的代码。


              string FILE_NAME = "Log" + System.DateTime.Now.Ticks.ToString() + "." + "txt"; 
              string str_Path = HostingEnvironment.ApplicationPhysicalPath + ("Log") + "\\" +FILE_NAME;
              
              
               if (!File.Exists(str_Path))
               {
                   File.Create(str_Path).Close();
                  File.AppendAllText(str_Path, jsonStream + Environment.NewLine);
              
               }
               else if (File.Exists(str_Path))
               {
              
                   File.AppendAllText(str_Path, jsonStream + Environment.NewLine);
              
               }
              

              【讨论】:

              • 看来上一个很完美。
              【解决方案8】:
              using (StreamWriter writer = File.AppendText(LoggingPath))
              {
                  writer.WriteLine("Text");
              }
              

              【讨论】:

                【解决方案9】:

                以上方法均无效,我自己找到了解决方案

                 using (StreamWriter wri = File.AppendText("clients.txt"))
                        {
                            wri.WriteLine(eponimia_txt.Text + "," + epaggelma_txt.Text + "," + doy_txt.Text + "," + dieuthini_txt.Text + ","
                                + proorismos_txt.Text + "," + poly_txt.Text + "," + sxePara_txt.Text + "," + afm_txt.Text + ","
                                + toposFortosis_txt.Text + ",");
                        }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-10-06
                  • 2014-04-21
                  • 2016-03-01
                  • 1970-01-01
                  • 2021-05-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多