Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
23 Oct 2007 1  
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