10. Create files with date and time stamp appended to the name

Note: Windows does not like ':' in file names. Also I made this extension friendly so it doesn't just add a date time stamp at the end of an html file for example and instead puts the stamp just before the '.'

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace lrn2CSharp10 { class Program { static void Main( string[ ] args ) { string fileName = ""; string timestamp = DateTime.Now.ToString( "MM-dd-yyyy_HH-mm-ss" ); Console.WriteLine( "Input name of new file in the current directory to create (it will have the current date appended to it)." ); Console.Write( ":" ); try { fileName = Console.ReadLine( ); if ( fileName.Contains( '.' ) ) { string scope = fileName.Split( '.' ).Last( ).ToLower( ); fileName = fileName.Replace( "." + scope, "_" + timestamp + "." + scope ); } else { fileName += "_" + timestamp; } FileStream fs = new FileStream( fileName, FileMode.Create ); fs.Close( ); } catch { Console.WriteLine( "WTF?!?" ); } } } }

posted by dharh 3:20 PM Sep 30th, 2010

9. Time and Date : Get system time and convert it in different formats 'DD-MON-YYYY', 'mm-dd-yyyy', 'dd/mm/yy' etc. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lrn2csharp9 { class Program { static void Main( string[ ] args ) { DateTime now = DateTime.Now; Console.WriteLine( "Today's date is: " + now.ToString( "MMM dd yyyy HH:mm:ss" ) ); Console.WriteLine( "Also: " + now.ToString( "dd-MMM-yyyy" ) ); Console.WriteLine( "And: " + now.ToString( "MM-d-yyyy" ) ); Console.WriteLine( "One more thing: " + now.ToString( "d/MM/yy" ) ); } } }

posted by dharh 3:03 PM Sep 30th, 2010


« Previous
1
Next »


2024: 1
2023: 4 2 1
2022: 5 3
2011: 5 3 1
2010: 12 9 7 1
2009: 12 11 8 5
2008: 12 5 4 3 2 1
2007: 12 11 10 9 8 7 6 5 4 3 2 1
2006: 12 11 10 9 8 7 6 5 4 3 2 1
2005: 12 10 7 6
2004: 10 9 6 5 4 3 2 1