简体   繁体   中英

Overwrite output in txt file C# in console application

if (string.IsNullOrEmpty(indata))
{
    StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true);
    sw.WriteLine("0");
    sw.Close();  
}

This is my code. How can I overwrite a result in arjun.txt file I need single result.

The true part means "append" - either just get rid of it entirely, in which case you'll use the StreamWriter constructor overload which overwrites by defalut, or change true to false .

Or preferably, just use:

File.WriteAllLines(@"c:\argun.txt", new[] {"0"});

When you can specify all the data in one go, the convenience methods in File are really helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM