How to truncate a file in c#?

前端 未结 8 1003
情深已故
情深已故 2020-12-01 23:44

I am writing actions done by the program in C# into a file by using Trace.Writeln() function. But the file is becoming too large. How to truncate this file when it grows to

相关标签:
8条回答
  • 2020-12-02 00:38

    Close the file and then reopen it using FileMode.Truncate.

    Some log implementations archive the old file under an old name before reopening it, to preserve a larger set of data without any file getting too big.

    0 讨论(0)
  • 2020-12-02 00:38

    As opposed to trying to do this yourself, I'd really recommend using something like log4net; it has a lot of this sort of useful functionality built in.

    0 讨论(0)
提交回复
热议问题