文件写入语句无法正常工作

Vic*_*tor 0 c# file-io file streamwriter streamwriter.write

我有以下功能:

static public void logEntry( string expression, string result )
{
    string content = expression + "=" + result + "\n";
    if (!logFileExists()) File.Create( logFileName );
    StreamWriter sw = new StreamWriter( logFileName );
    sw.Write( content );
    sw.Close();
}
Run Code Online (Sandbox Code Playgroud)

......在这三个电话之后:

logEntry( "3/2", "1.5");
logEntry( "8/2", "4");
logEntry( "10/2", "5");
Run Code Online (Sandbox Code Playgroud)

我的文件看起来像这样:

10/2=5
Run Code Online (Sandbox Code Playgroud)

而不是这个:

3/2=1.5
8/2=4
10/2=5
Run Code Online (Sandbox Code Playgroud)

我能做什么?

Ste*_*eve 5

如果文件存在,请使用附加的StreamWriter构造函数重载

using(StreamWriter sw = new StreamWriter( logFileName, true ))
{
     sw.Write( content );
}
Run Code Online (Sandbox Code Playgroud)

此外,如果文件不存在,则此构造函数会创建一个新文件,因此不需要调用File.Create.