Refael
Refael

Reputation: 7323

C# How to get the date that the file was put into a folder?

Assume, on Sunday file.txt file is created, saved in folder folder1.

On Monday, someone copied or moved file.txt to a folder folder2.

On Tuesday, For file Folder2/file.txt, I want to get the date when the file came to folder2 (ie date Monday)

UPDATE: FileInfo.LastAccessTime prop, is not affected when moving a file from folder to another, but only when copy paste file.

Upvotes: 3

Views: 2476

Answers (3)

Mitchell Chu
Mitchell Chu

Reputation: 137

Action        CreationTime       LastWriteTime      LastAccessTime            FullName
        2016/2/17 23:32:09  2016/2/17 23:43:06  2016/2/17 23:43:06         D:\Temp\tmp
CopyTo  2016/3/16 17:57:00  2016/2/17 23:43:06  2016/3/16 17:57:00   D:\Temp\Test\tmp1
MoveTo  2016/2/17 23:32:09  2016/2/17 23:43:06  2016/2/17 23:43:06   D:\Temp\Test\tmp2

I use FileInfo to test the file. CopyTo will change the CreationTime and LastAccessTime, but MoveTo not change any of the three attributes.

When the file be copied to destination folder, we can use CreationTime to detect the copied time. I have no idea to detect the time when the file was moved, maybe the folder's LastAccessTime can be used sometimes.

If it is possible, use FileSystemWatcher to watch for changes in a special directory, then save the time of file changed.

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

You can use File.SetCreationTime to set copied time.

File.Move(sourceFile, destinationFile);
File.SetCreationTime(destinationFile, DateTime.Now);

Update :

Since question is updated to know (only)date when the file is copied manually, we just need to know when the file was Created or LastAccessed. You could do this.

var f = new FileInfo("destinationfile");
DateTime lastAccess = f.LastAccessTime
string datoftheweek = lastAccess.ToString("ddd");

Upvotes: 2

Thiru
Thiru

Reputation: 3373

You can use the FileSystemInfo.LastAccessTime Property

Refer to the msdn link https://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastaccesstime(v=vs.110).aspx

Upvotes: 2

Related Questions