["$file..G$ ","|date as filename$filename property$","HYVXKVVusing System;Vusing System.IO;VVclass ProgramV{VHstatic KDates and required filenamesKVVDate:HHH 11/18/2008 5:04:44 PMVRequired filename: text-2008-11-18_05-04-44-PM.binVVDate:HHH 11/23/2004 12:05:47 AMVRequired filename: text-2004-11-23-12_05-47-AM.binKYvoid Main()VH{KVHH//VHH// Write file containing the date with BIN extensionVHH//VHHKstring n = string.Format(KXtext-{0:yyyy-MM-dd_hh-mm-ss-tt}.binXK,VHHHDateTime.Now);VHHFile.WriteAllText(n, XaaaX);VH}V}VVKDescription of format stringKVVXtext-{0:yyyy-MM-dd_hh-mm-ss-tt}.binXVVtext-H The first part of the output requiredVHH Files will all start with text-V{0:HHIndicates that this is a string placeholderVHH The zero indicates the index of the parameters inserted hereVyyyy-H Prints the year in four digits followed by a dashVHH This has a Xyear 10000X problemVMM-HHPrints the month in two digitsVdd_HHPrints the day in two digits followed by an underscoreVhh-HHPrints the hour in two digitsVmm-HHPrints the minute, also in two digitsVss-HHAs expected, it prints the secondsVttHH Prints AM or PM depending on the time of dayKYstring SpecialFileNameVH{VHHgetVHH{KVHHH// AVHHHKreturn string.Format(X{0}{1}text-{2:yyyy-MM-dd_hh-mm-ss-tt}.binX,KVHHHH// BVHHHHKEnvironment.GetFolderPath(Environment.SpecialFolder.MyDocuments),KVHHHH// CVHHHHKPath.DirectorySeparatorChar,KVHHHH// DVHHHHKDateTime.Now);VHH}VH}VVHstatic void Main()VH{VHHConsole.WriteLine(SpecialFileName);VH}V}VVKVVC:\\Users\\Sam\\Documents\\text-2008-11-18_05-23-13-PM.binK","A*BEDrAB.CP1B| 64F(B 6666}(CCE(XCE/C","Filename, DateTime."," A filename can store the date and time. This clearly identifies the file in a unique way. There are many ways of accomplishing this. Some are clearer to read and easier to modify than others. We include the date, or the date and the time. ","You can use DateTime formatting for filenames. A format string is provided.","Example."," We use the current date and time as the name for a file on the file system. We call the string.Format method, and combine it with DateTime.Now, for a method that outputs the correct string based on the date and time. ","DateTime.Now ","datetime-now","Environment.SpecialFolder."," You can use this code with Environment.SpecialFolder. You can encapsulate the logic for this in a property accessor. In other words, you can use a property to easily get the complete filename. ","Property ","property","This example"," uses a format string in part A. This format string has three placeholders: first, the directory name we get from SpecialFolder, then the separator char, and finally the specially formatted DateTime. ","The code specifies that the file name starts with text and ends with \".bin\".","Next: ","It gets the My Documents folder path. This line uses the SpecialFolder.MyDocuments enum value to get the path.","Environment ","environment","Finally,"," it uses Path.DirectorySeparatorChar. On Windows, this is the backslash. You could just use \\\\ in the string instead. It uses DateTime.Now. As expected, this returns the local time.","Separators."," You can change the string separators by replacing the underscores and dashes with any other character, excluding some special ones. I haven't found many uses for separators other than dashes, underscores, and spaces. ","Filenames cannot have some characters. This is explored further in a separate article.","Reserved Filenames ","reserved-filenames","Summary."," We generated file names containing the current date. This is useful for \"data dumps\" and log files where daily files are written. Never use random names except when you need temporary files. It is often better to append the date. ","If you have to write files frequently, using the \"ss\" field can help\u2014this writes seconds."]