HI guys,
I only started c# recently so I’m not 100% familiar with it’s syntax yet and I got into a problem. I’d like to write the current time into a filename. I’m using the following code:
DateTime now = DateTime.now;
string dateString = string.Format(@"Z:\test\{0}.bmp",now.ToString("s"));
bitmap.Save(dateString);
Now this gives me a can’t access filepath error. Apparently it has something to do with the “:” characters at the time part (at least when I give a now.ToString(“d”) ) it saves fine.
Any idea whats causing this? Thanks.
The “s” format will create a filename of something like:
That’s just not a valid filename, due to the colon. Either replace the colon with another character after calling ToString, or use a different format.
Note that “d” won’t always work either, as it can include “/” in the name, depending on the culture.
Personally I’d suggest something like “yyyyMMdd-HHmmss” which will give you something like