-1

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.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
toni
  • 133
  • 1
  • 13

3 Answers3

1

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(); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

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.

Skrface
  • 1,388
  • 1
  • 12
  • 20
0

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

Icculus018
  • 1,018
  • 11
  • 19