Click here to Skip to main content
16,013,516 members
Articles / Desktop Programming / Windows Forms
Article

Changing file properties such as date of creation, modification and last access

Rate me:
Please Sign up or sign in to vote.
2.35/5 (6 votes)
23 Oct 20071 min read 38K   976   12   2
This application will help to change file properties such as date of creation, modification and last access.

Introduction

This application will help thoes who wants to change file properties such as date of creation, modification and last acess date.

Background

I was also in search of ultility which will help me to change any files properties such as date of creation, modifcation and last access date. I found many but only the problem was if I changed date of file by using those application, it will add text such as this date has been changed by using so and so application. I done more research and finally I found this solution.

Using the code

Basically for changing these file attributes we need to include follwing namespace.

//using System.IO;

For developing this application we need openFileDialog box, which will provide file name with its physical location. Then this file passed as a parameter to FileInfo which provide the entire properties related to file. FileInfo provides functionality to get and set value for certain properties such as date of Creation, Modification and Last Access.

private void button1_Click(object sender, EventArgs e)
       {
           openFileDialog1.ShowDialog();
           txtFileName.Text = openFileDialog1.FileName;
           if (txtFileName.Text.Trim() != "")
           {
               string fn = openFileDialog1.FileName;
               FileInfo fileIn = new FileInfo(fn);
               lblOriCreationDate.Text = fileIn.CreationTime.ToString();
               lblOrigModifiedDate.Text = fileIn.LastWriteTime.ToString();
               lblOrigLastAccessDate.Text = fileIn.LastAccessTime.ToString();
               pnlChangingAtt.Visible = true;
           }
       }

On button1_Click open file dialog box, then select file. It will show file name in textBox and its respective dates. Now we want to change dates, just select date from respective DateTimePicker and click on change. This change all respective date and show it below Changed File Information.

Internally on button1_Click it will get file name and based on that it will get FileInfo.

private void btnChange_Click(object sender, EventArgs e)
       {
           if (txtFileName.Text.Trim() != "")
           {
               string fn = openFileDialog1.FileName;
               FileInfo fileIn = new FileInfo(fn);
               ChangeFileAttribute(fileIn);
               MessageBox.Show("File information has been changed");
           }
       }
       public void ChangeFileAttribute(FileInfo Fln)
       {
           Fln.CreationTime =dtpCreationDate.Value.Date;
           Fln.LastWriteTime = dtpModification.Value.Date;
           Fln.LastAccessTime=dtpLastAccessDate.Value.Date;
           lblChangedCreated.Text = Fln.CreationTime.ToString();
           lblChangedModified.Text = Fln.LastWriteTime.ToString();
           lblChangedAccessDate.Text = Fln.LastAccessTime.ToString();

       }

After getting FileInfo just get respective properties as CreationTime, LastWriteTime and LastAccessTime set those propeties with desire dates.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead iGate, India
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTime stamp Pin
Member 79730511-Jun-11 12:47
Member 79730511-Jun-11 12:47 
GeneralChanging file properties such as date of creation, modification and last access Pin
kimberly001-Mar-10 5:35
kimberly001-Mar-10 5:35 
Hi There,

Thanks for sharing this application. Almost what I wanted except..time will always be 12 AM. Do you have anyway to customize the time too? Otherwise it will be too obvious.

Cheers

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.