Arnet11
Arnet11

Reputation: 79

How to get last modified date of file while moving it using C#

I have a utility that move (system.io.move) files from one folder to another folder. When file is moved it's created date and modified date does not change (it changes when copy paste is done). I need to identify which files were moved on a particular date. Kindly note that files have already been moved.

Upvotes: 0

Views: 2726

Answers (4)

tinstaafl
tinstaafl

Reputation: 6958

As was mentioned finding out which files were moved at a specific time after they've been moved isn't available. However you can prevent this in the future by setting one of the datetime properties through the FileInfo class. You must have at least the full path of the file when you move it. Create a FileInfo object from that and change the appropriate datetime property. I would suggest something that you probably won't need like the LastAccessTime.

FileInfo f1 = new FileInfo("Text.txt");
//readonly must be false to change the property.  It can be changed back if needed.
f1.IsReadOnly = false;
File.Move(f1.FullName, @"C:\test\" + f1.Name);
f1 = new FileInfo(@"C:\test\" + f1.Name);
f1.LastAccessTime = DateTime.Now;

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83403

That is not possible.

There is a file modified date, but if they are not changed (i.e. their bytes are not changed), the date is not reset.

Of course, in the case of copy & paste, the pasted files have their created and modified date resets (bytes changed). The originals have no date reset.

Upvotes: 1

Mojtaba Homayoun
Mojtaba Homayoun

Reputation: 57

You can use this :

            //Sets the date and time the file was created.
        System.IO.File.SetCreationTime(@"F:\myFile.txt", DateTime.Parse("12/19/2010"));

        //Sets the date and time, in coordinated universal time (UTC), that the file was created.
        System.IO.File.SetCreationTimeUtc(@"F:\myFile.txt", DateTime.Now);

Upvotes: 1

arviman
arviman

Reputation: 5255

If you don't mind recreating the files, just make a function which creates a new file and copies it's contents over. If your file size is very big, use buffers but if your files are reasonably small, this should do fine.

void CopyContentOfFile(string file1, string file2)
{
    byte[] val = File.ReadAllBytes(file1);
    File.WriteAllBytes(file2, val);
}
CopyContentOfFile("test1.txt","test2.txt"); //copy test1 to test2.

Upvotes: 0

Related Questions