Dot Net Perls
Top 37 C# Example Pages

[",twrhdsyt..CY1BST~~}T~~P64F(BP6666F(CCE(XCE/CS}T~~}T~~","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. ","Tip: ","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","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","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","Output","\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. ","Also: ","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. ","Note: ","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. ","Tip: ","If you have to write files frequently, using the \"ss\" field can help\u2014this writes seconds.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABBCAMAAABYbu5jAAAAMFBMVEX///+1wv+jqv9siv/p6//W3f8zZv9gb//19v9NK/8zAP999//S//8z//8zzP+z6P/0gInFAAABg0lEQVR4XtXXy1ICURAD0H7c96D+/9/KQqkhlEKD0iTrWZzKYtJXbou2s+jvn1Q55f2mfAMOb6Ec5D6+c/ObcvOdm98qN9+5+a1w842b3wo33/6Dv2H22g3zCL+PKF/uSK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