Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The given path's format is not supported" when trying to create file based on current time

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...

like image 697
João Silva Avatar asked Nov 18 '13 14:11

João Silva


1 Answers

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 ...

like image 98
marc_s Avatar answered Nov 04 '22 20:11

marc_s