Trying to make series of timestamped files; throwing DirectoryNotFound exception

I’m collecting some data from my game, and I want to write it to a file. I’m using a streamwriter, and it worked when all I wanted it to do was write the data to a file.

eg. path = “/this/is/a/filepath/data.csv”

However, when I wanted to add a timestamp, so that it would create a set of unique files

eg.
stamp = DateTime.Now;
path = “/this/is/a/filepath/data” + stamp.ToString(“s”) + “.csv”

It starts throwing a DirectoryNotFound exception as well as NullReferenceExceptions (which it did not before), but I’m pretty sure that is a product of this filepath issue. Why might this be happening?

You can’t use that format for DateTime in file names as far as I’m aware. It includes the ‘:’ symbol. I’d pick something like “yyyMMdd”.

I know @Scabbage tried to reply, but maybe it wasn’t precise enough. So, your problem probably is that the date format contains ‘/’ characters. Which becomes “/this/is/a/filepath/data” + “03/28/2018” + “.csv” or something like that.
The plain and simple solution is to replace these characters.

stamp = DateTime.Now;
path = "/this/is/a/filepath/data-" + stamp.ToString("s").Replace("/", "-") + ".csv";

Thanks. The character it didn’t like was colons. I initially thought it had something to do with static/nonstatic, but I started off removing the wrong characters.

If it’s complicated, you can use other format: https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

Yeah, that’s what I meant. Colons are a reserved character.