Thursday, August 29, 2019

Get the date when a file was last modified in C#!

using System;
using System.Collections.Generic;
using System.IO;
namespace FileCrawling
{
   class Program
   {
      static void Main(string[] args)
      {
         Dictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>();
         string whereAmI = @"C:\\DummyDirectory\";
         foreach (string filePath in Directory.GetFiles(whereAmI))
         {
            string fileName = filePath.Replace(whereAmI, "");
            DateTime modifiedDate = File.GetLastWriteTime(filePath);
            dictionary.Add(fileName,modifiedDate);
         }
         Console.Write("Files:\r\n");
         foreach (KeyValuePair<string, DateTime> keyValuePair in dictionary)
         {
            Console.Write(keyValuePair.Key + " unalt since " + keyValuePair.Value + "\r\n");
         }
         Console.WriteLine("\r\nPress any key to end.");
         Console.ReadKey(true);
      }
   }
}

 
 

Note that this will not crawl subfolders for files. Use Directory.GetDirectories for that per: this

No comments:

Post a Comment