Reputation: 11
My C# code is generating a text file with a content that is being fetched from the db and saving those in a folder. Every time when I run the code, it overrides the existing file.
Here is my code.
StreamWriter file = new StreamWriter(@"C:\ExportedData\myFile.txt");
file.WriteLine(sb.ToString());
file.Close();
Ideally, I would want to keep all the files and append each file's name with current date and time.
Upvotes: 1
Views: 662
Reputation: 3847
You can use the DateTime.Now.ToString()
method to generate a string representation of the current date and time and include that in the filename you're producing. The $ at the start of this string lets you add parameters inline using variables or code expressions inside curly brackets.
Console.WriteLine($@"C:\ExportedData\myFile_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt");
Upvotes: 1