-1

I simply want to write data from a listBox to a textfile. The name of the textfile should include the current date and time.

string filename = String.Concat(string.Format("{0:yyyy-MM-dd}", DateTime.Today), "_", string.Format("{0:HH:mm:ss}", DateTime.Now), ".txt");
System.IO.StreamWriter fs = new System.IO.StreamWriter(filename, false);
foreach (var item in myLbx.Items)
{
    fs.WriteLine(item);
}
fs.Close();

When I run this code, I get a NotSupportedException, saying that the format of my filename is not supported.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Boozzz
  • 245
  • 1
  • 6
  • 19
  • you must use path.Combine(...) – Charlie Jul 18 '14 at 07:03
  • 4
    First of all it would really help if you could show us the actual file name. And second you could try creating a file in the Explorer containing a ':' character (I'm assuming your current culture uses that as time seperator) and see what happens. – Dirk Jul 18 '14 at 07:05
  • filename = "2014-07-18_09:12:33.txt" – Boozzz Jul 18 '14 at 07:13
  • And yes, you are right. It seems that ":" is not so cool. Thank you!! – Boozzz Jul 18 '14 at 07:15

1 Answers1

2

You are trying to create a file with a name that has reserved characters, such as : or < in the filename. You must use another character for your colons.

Daniel B
  • 8,770
  • 5
  • 43
  • 76