["$file..E$ ","A)BDrAB.CP1BP64F(BP6666}(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.","Dates and required filenames","\n\nDate: 11/18/2008 5:04:44 PM\nRequired filename: text-2008-11-18_05-04-44-PM.bin\n\nDate: 11/23/2004 12:05:47 AM\nRequired filename: text-2004-11-23-12_05-47-AM.bin","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","C# program that uses date as filename","\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {","\n //\n // Write file containing the date with BIN extension\n //\n ","string n = string.Format(","\"text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin\"",",\n DateTime.Now);\n File.WriteAllText(n, \"aaa\");\n }\n}\n\n","Description of format string","\n\n\"text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin\"\n\ntext- The first part of the output required\n Files will all start with text-\n{0: Indicates that this is a string placeholder\n The zero indicates the index of the parameters inserted here\nyyyy- Prints the year in four digits followed by a dash\n This has a \"year 10000\" problem\nMM- Prints the month in two digits\ndd_ Prints the day in two digits followed by an underscore\nhh- Prints the hour in two digits\nmm- Prints the minute, also in two digits\nss- As expected, it prints the seconds\ntt Prints AM or PM depending on the time of day","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","C# program that uses filename property","\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n static string SpecialFileName\n {\n get\n {","\n // A\n ","return string.Format(\"{0}{1}text-{2:yyyy-MM-dd_hh-mm-ss-tt}.bin\",","\n // B\n ","Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),","\n // C\n ","Path.DirectorySeparatorChar,","\n // D\n ","DateTime.Now);\n }\n }\n\n static void Main()\n {\n Console.WriteLine(SpecialFileName);\n }\n}\n\n","\n\nC:\\Users\\Sam\\Documents\\text-2008-11-18_05-23-13-PM.bin","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."]