【问题标题】:How to add new line into txt file如何在txt文件中添加新行
【发布时间】:2012-01-05 12:47:06
【问题描述】:

我想在我的 date.txt 文件中添加带有文本的新行,但不是将其添加到现有的 date.txt 中,而是应用正在创建新的 date.txt 文件..

TextWriter tw = new StreamWriter("date.txt");

// write a line of text to the file
tw.WriteLine(DateTime.Now);

// close the stream
tw.Close();

我想打开 txt 文件,添加一些文本,然后将其关闭,然后单击某些内容后:打开 date.txt,添加文本,然后再次关闭它。

所以我可以得到:

按下按钮:txt 打开 -> 添加当前时间,然后关闭它。按下另一个按钮,打开 txt -> 在同一行添加文本“OK”或“NOT OK”,然后再次关闭。

所以我的 txt 文件将如下所示:

2011-11-24 10:00:00 OK
2011-11-25 11:00:00 NOT OK

我该怎么做?谢谢!

【问题讨论】:

    标签: c# winforms stream text-files


    【解决方案1】:

    你可以很容易地做到这一点

    File.AppendAllText("date.txt", DateTime.Now.ToString());
    

    如果你需要换行符

    File.AppendAllText("date.txt", 
                       DateTime.Now.ToString() + Environment.NewLine);
    

    无论如何,如果您需要您的代码,请执行以下操作:

    TextWriter tw = new StreamWriter("date.txt", true);
    

    第二个参数告诉追加到文件。
    检查here StreamWriter 语法。

    【讨论】:

    • 如果您使用的是 c# 4(或更新版本)编译器,您可以输入 new StreamWriter("date.txt", append:true) 以使意图更清晰。
    【解决方案2】:

    没有换行:

    File.AppendAllText("file.txt", DateTime.Now.ToString());
    

    然后在OK后换行:

    File.AppendAllText("file.txt", string.Format("{0}{1}", "OK", Environment.NewLine));
    

    【讨论】:

    【解决方案3】:

    为什么不使用一个方法调用来做到这一点:

    File.AppendAllLines("file.txt", new[] { DateTime.Now.ToString() });
    

    它将为您执行换行,并允许您根据需要一次插入多行。

    【讨论】:

    • 我更喜欢这个而不是接受的答案;你不需要指定新行
    【解决方案4】:
    var Line = textBox1.Text + "," + textBox2.Text;
    
    File.AppendAllText(@"C:\Documents\m2.txt", Line + Environment.NewLine);
    

    【讨论】:

      【解决方案5】:

      尝试使用以下代码在 Unity3D 中创建内容并将其写入文本文件。

      void CreateLog() {
              string timestamp = DateTime.Now.ToString("dd-mm-yyyy_hh-mm-ss");
              PlayerPrefs.SetString("timestamp", timestamp);
              string path = Application.persistentDataPath + "/" + "log_" + timestamp + ".txt";
              // This text is added only once to the file.
              if (!File.Exists(path)) {
                  // Create a file to write to.
                  using (StreamWriter sw = File.CreateText(path))
                  {
                      sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
                  }   
              } else {
                  // This text is always added, making the file longer over time
                  // if it is not deleted.
                  using (StreamWriter sw = File.AppendText(path)) {
                      sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
                  }   
              }
      
              // Open the file to read from.
              using (StreamReader sr = File.OpenText(path)) {
                  string line = "";
                  while ((line = sr.ReadLine()) != null)
                  {
                      Debug.Log(line);
                  }
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多