Click here to Skip to main content
15,884,794 members
Articles / Programming Languages / C#
Article

Change File Date Attributes

Rate me:
Please Sign up or sign in to vote.
4.48/5 (23 votes)
27 Nov 2008CPOL1 min read 185K   11.1K   41   23
Change created, modified and accessed time

Introduction

Have you ever found yourself in a situation where you wanted to change the attributes of a file such as the creation date, modified date or last accessed date.

For one reason or another, sometimes you might just want to change the attributes, and since Windows does not offer an easy way to do so, I wrote a small easy-to-use program to change the date of a file.

Using the Program

After extracting and executing the program, click open and load the file for which you want to change the attributes.

ChangeFileDateAttributes_Open.png

After you open the file, you will see its path in the textbox in the bottom of the program.
Now all you have to do is just click on the checkbox that you wish to edit, and select a new time and date.

ChangeFileDateAttributes_Modify.png

Click Save and the new attributes will be saved into the file.

ChangeFileDateAttributes_Save.png

The file attributes will look like the following:

ChangeFileDateAttributes_Properties.png

Using the Code

I will explain just the functions required to change the date attributes.

First we add the IO library:

C#
using System.IO;

Then we get the path of the file:

C#
string path="";

OpenFileDialog dialog = new OpenFileDialog();

if (dialog.ShowDialog() == DialogResult.OK)
{
	path = dialog.FileName;
}

Then we create the DateTime object:

C#
DateTime dtCreation = new DateTime(2007, 10, 2, 2, 19, 33);
DateTime dtModified = new DateTime(2008, 11, 4, 2, 23, 4);

And we assign it to the file:

C#
//Change the file created time.
File.SetCreationTime(path, dtCreation);
//Change the file modified time.
File.SetLastWriteTime(path, dtModified);

Conclusion

I wrote this small program for a friend of mine who wanted to change the date of a file and didn't know any easy way to do it. Please feel free to reply or ask any question(s).

History

  • 27th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAdding AM/PM Pin
stinking_shoe7-Jul-22 8:55
stinking_shoe7-Jul-22 8:55 
QuestionDoes file.copy function change modified date? Pin
Member 1079034826-Jun-19 3:50
Member 1079034826-Jun-19 3:50 
GeneralComment Pin
Member 1275503516-Mar-17 19:12
Member 1275503516-Mar-17 19:12 
GeneralGreat................ Pin
Ganesh Madane28-Aug-16 19:46
Ganesh Madane28-Aug-16 19:46 
Thanks for this great article.Best luck.
QuestionEnhencments Pin
Khursheed Fateh17-Aug-16 18:41
Khursheed Fateh17-Aug-16 18:41 
Questionautomatically changing a filename each time I save a file in visual studio. Pin
Member 1194056227-Aug-15 5:07
Member 1194056227-Aug-15 5:07 
GeneralMy vote of 5 Pin
Member 1111263626-Sep-14 7:54
Member 1111263626-Sep-14 7:54 
SuggestionThank you (extra code to enumerate a folder) Pin
zhivot3-Sep-14 15:18
zhivot3-Sep-14 15:18 
GeneralMuchas gracias! Pin
Member 1000395021-Apr-13 22:34
Member 1000395021-Apr-13 22:34 
GeneralThanks.. works great Pin
saumen_halder3-Apr-13 0:58
saumen_halder3-Apr-13 0:58 
QuestionPowershell Option Pin
oolie30-Mar-13 19:54
oolie30-Mar-13 19:54 
GeneralAdding AM or PM to CustomFormat Pin
John Jasper13-Aug-09 11:38
John Jasper13-Aug-09 11:38 
QuestionHow to change last saved date? Pin
Member 46863649-Jul-09 0:14
Member 46863649-Jul-09 0:14 
AnswerRe: How to change last saved date? Pin
oolie30-Mar-13 19:48
oolie30-Mar-13 19:48 
GeneralChanging more than one file's date. Pin
dbett8-Feb-09 16:35
dbett8-Feb-09 16:35 
GeneralRe: Changing more than one file's date. Pin
Etienne Rached17-Feb-09 13:55
Etienne Rached17-Feb-09 13:55 
Generalexecuting the program Pin
jim sladge6-Jan-09 5:12
jim sladge6-Jan-09 5:12 
GeneralRe: executing the program [modified] Pin
Etienne Rached10-Jan-09 15:54
Etienne Rached10-Jan-09 15:54 
QuestionConsistency of dates ? Pin
rob tillaart2-Dec-08 22:14
rob tillaart2-Dec-08 22:14 
AnswerRe: Consistency of dates ? Pin
Dennis Dykstra5-Dec-08 4:19
Dennis Dykstra5-Dec-08 4:19 
AnswerRe: Consistency of dates ? Pin
Etienne Rached16-Dec-08 17:52
Etienne Rached16-Dec-08 17:52 
GeneralRe: Consistency of dates ? Pin
rob tillaart16-Dec-08 19:44
rob tillaart16-Dec-08 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.