Is there any way to save a file with a variable name?
For example, I have this code but I got error in the StreamWriter
:
string hour = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Now.ToShortDateString();
string filename = "Numbers " + date + " " + hours+ ".txt";
string path = @"C:\ShowMySms\" + filename;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true)) {
file.WriteLine("test");
}
I also tried use
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ShowMySms\" + filename, true))
but both ways give me an error.
Is there any way for me to export files with a different using StreamWriter
?
Thank you in advance!
Edit: the error is
The given path's format is not supported.
I tried do
Path.Combine(path, filename);
and put the path in the StreamWriter
but still getting the same error...
Try using this formatting instead:
string filename = string.Format("Numbers_yyyyMMdd_HHmmss.txt", date);
This will give you file names like
Numbers_20131118_155410.txt
which contain no "dangerous" and illegal characters (like :
) in your file name ...