Subscribe to gbertoli3's Programming, WebDesign, and Misc Blog        RSS Feed
-----

Change File's Creation Date

Icon Leave Comment
Have you ever wanted trick your friends into believing that you created a Text File the day after you were born? When you tell them that you created a Text File the day after you were born, they are probably thinking that you are just kidding, until you show them the File's Creation, Last Accessed, and Last Written Date. You should title the File something like MyFirstTextFile.txt

Now for the Code and Explanation.

What this code does is it changes the File's Creation, Last Accessed, and Last Written Date to the Date you specify. Your friends won't believe you, but for some odd reason, they will.

        private void changeFileDate(String filename, DateTime newDate)
        {
            //Try to change the Creation Time
            try
            {
                //Here we are setting the Creation Time to the
                //specified date called newDate, for the file
                //called filename
                System.IO.File.SetCreationTime(filename, newDate);
            }
            catch
            {
                MessageBox.Show("There seems to be an error with the specified FileName or the specified Date.", "Error");
            }
            //Try to change the Last Write Time to our CreationTime
            try
            {
                //Here we are setting the Last Write Time to the Creation Date
                System.IO.File.SetLastWriteTime(filename, newDate);
            }
            catch
            {
                MessageBox.Show("There seems to be an error with the specified FileName or the specified Date.", "Error");
            }
            //Try to change the Last Accessed Time to our CreationTime and to our LastWriteTime
            try
            {
                //Here we are setting the Last Accessed Time to the Creation Date and the Last Write Time
                System.IO.File.SetLastAccessTime(filename, newDate);
            }
            catch
            {
                MessageBox.Show("There seems to be an error with the specified FileName or the specified Date.", "Error");
            }
        }



Enjoy!

0 Comments On This Entry

 

Search My Blog

September 2014

S M T W T F S
 123456
78910111213
14151617181920
21 222324252627
282930    

Tags

    Recent Entries

    Recent Comments

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)