English 中文(简体)
如何将新线添加到txt档案中
原标题:How to add new line into txt file

我现在要补充与案文的新行文。 txt文档,但不将其添加到现有日期。 t,正在创造新的日期。 txt file.

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

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

// close the stream
tw.Close();

我愿开放txt文档,添加一些案文,将其关闭,然后点击:公开日期.txt,增加案文,并再次关闭。

因此可以做到:

Button Pressed: txt open -> supplemented current time, 然后关闭。 另有一家纽特公司在同一个行文中刊登了“K”或“NOT OK”,随后又将其关闭。

So my txt file will look like that:

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

如何做到这一点? 感谢!

最佳回答

You could do it easily using

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

如果你需要新的路线,

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

如果你需要你的守则,那么:

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

with second parameter telling to append to file.
Check here StreamWriter syntax.

问题回答

无新行:

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

之后再到科索沃再进入一条新线:

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

为什么不采用一种方法:

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

which will do the newline for you, and allow you to insert multiple lines at once if you want.

var Line = textBox1.Text + "," + textBox2.Text;

File.AppendAllText(@"C:Documentsm2.txt", Line + Environment.NewLine);

要求颁布以下法典,以制作内容并写到统一3D的案文档案中。

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);
            }
        }
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签