0

I have the following function:

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

...after these three calls:

logEntry( "3/2", "1.5");
logEntry( "8/2", "4");
logEntry( "10/2", "5");

my file looks like this:

10/2=5

instead of this:

3/2=1.5
8/2=4
10/2=5

What could I do?

4

1 回答 1

5

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

using(StreamWriter sw = new StreamWriter( logFileName, true ))
{
     sw.Write( content );
}

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

于 2012-11-04T19:22:26.653 回答