I am writing some code for a program that archives files. So I need to find files which were last modified one year ago.

string[] as_Datien = Directory.GetFiles(s_Pfad, "*.*", SearchOption.AllDirectories);

for (int i_Stelle = 0; i_Stelle < as_Datien.GetLength(0); i_Stelle++)
{
}

I want to check if a file was last modified 1 year ago.

3 Answers

0
Skrface On

You are probably looking for the File.GetLastWriteTime() method. It returns the date and time of the last edition of files and/or folders.

Check the doc out.

0
kristech On

As has already been mentioned use File.GetLastWriteTime() and check that date against today's date one year ago using DateTime.Now.AddYears(-1)

1
Dmitry Bychenko On

You can try using Linq and FileInfo to get file's last modification date:

  DateTime threshold = DateTime.Now.AddYears(-1);

  // files which was modified earlier than 1 year ago
  string[] as_Datien = Directory
    .EnumerateFiles(s_Pfad, "*.*", SearchOption.AllDirectories)
    .Where(file => new FileInfo(file).LastWriteTime < threshold)
    .ToArray();