StreamWriter – how not to clear existing text
StreamWriter has some options of usage. You can as add new text to an existing text in file, so and to clean existing text and add a new one.
The 6th option of usage StreamWriter is (string path, bool append, Encoding encoding)
To truncate data in a file, you should use something like this:
using (StreamWriter sw = new StreamWriter(cmdFilePath, false, Encoding.Default))
{
String presentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
sw.WriteLine(text);
sw.Close();
}
If you want to add new text to a file, use true instead of false:
using (StreamWriter sw = new StreamWriter(cmdFilePath, true, Encoding.Default))
{
String presentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
sw.WriteLine(resultCommands);
sw.Close();
}

A Hidden Visual Studio Trick: Execute Selected Code with double Ctrl+E
What do to with Zone.Identifier files after importing a WSL machine and copying data
Support for SharePoint Server 2019 ends on Tuesday, July 14, 2026