0

I get the latest modified file from a directory using code below:

String tmpPath="C:\demotestDirectory";
FileInfo newestFile = GetNewestFile(new DirectoryInfo(tmpPath));
if (newestFile != null)
{
   DateTime lastmodifiedDate = newestFile.LastAccessTime;
   string currentMonth = DateTime.Now.Month.ToString();
}

And i get the latest modified filed from the directory , now i want to tell whether that file has been used or not in the past week, and also in the past month.

Any help appreciated.

chollida
  • 7,834
  • 11
  • 55
  • 85
confusedMind
  • 2,573
  • 7
  • 33
  • 74

3 Answers3

0

Use this to find 7 days ago:

DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));

Note that you're not getting last time modified. You're getting last time accessed. For last modification time use: LastWriteTime.

Or: by your comments, something like:

DateTime.Now.Subtract(new TimeSpan((int)DateTime.Now.DayOfWeek, 0, 0, 0));
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • i dont think that sounds write as 7 days is not what i am looking for, its past week and past month! So i cannot use 7 days or 30 days. – confusedMind Jun 02 '13 at 14:18
  • Whats the difference? will last time modified wont be the same as last time accessed? – confusedMind Jun 02 '13 at 14:19
  • @confusedMind Why is this different from month - which you wrote in a comment that you subtract 30 days? – ispiro Jun 02 '13 at 14:20
  • @confusedMind No. If it was read but not changed - it was not modified. – ispiro Jun 02 '13 at 14:20
  • because suppose date is 25/10/2012 and last 30 days = maybe 25/9/2012 .And the file was modified on 5/9/2012 so its still past month but wont be in the condition. – confusedMind Jun 02 '13 at 14:22
  • @confusedMind Correct. And same for week. – ispiro Jun 02 '13 at 14:23
  • Is there a way to get number of week from the year ? So i can check the number and subtract to see!? – confusedMind Jun 02 '13 at 14:25
  • @confusedMind Just use this in a simple Winforms application: `Text = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)).ToString();` – ispiro Jun 02 '13 at 14:26
  • @confusedMind Or do you mean you want the days from Sunday till today? – ispiro Jun 02 '13 at 14:27
  • Yes i want in a week , so if it is Tuesday it just checks till sunday . So Sunday - Tuesday just three days. – confusedMind Jun 02 '13 at 14:29
  • @confusedMind See http://stackoverflow.com/a/1665856/939213 only you'll need to subtract instead of add. Also see http://stackoverflow.com/a/38049/939213 – ispiro Jun 02 '13 at 14:34
0
FileInfo fi = new FileInfo(/*filename*/);
DateTime dateFile = fi.LastWriteTime;
DateTime now = DateTime.Now;

if (now.Year == dateFile.Year) { //same year?
   if (now.Month == dateFile.Month) { //same month?
       MessageBox.Show("File has been edited in this month.");
       DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
       Calendar c = dfi.Calendar;
       int fileWeek = c.GetWeekOfYear(dateFile, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
       int nowWeek = c.GetWeekOfYear(now, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
       if (fileWeek == nowWeek) { //same week?
           MessageBox.Show("File has been edited in this week.");
       }
   }

}

This code first checks if the file was edited in the same year. The it checks its month. Then it uses the calendar class with the current DateTimeInfo(this contains things like: How many days are in one week, which is the first day of week, etc.). The function GetWeekOfYear returns the number of the week. The two integers are compared, and thre you go!

Note:

You used the LastAccessTime, but this is also updated, when you do little things on the file like click it in the explorer(so not very helpful if you want to know if the user realy opened it). Use LastWriteTime instead(This changes if the file was changed).

Sébastien Garmier
  • 1,263
  • 9
  • 16
0

you can do this :

private void fileUsage()
 {
   String tmpPath = "C:\\demotestDirectory";
    FileInfo newestFile = GetNewestFile(new DirectoryInfo(tmpPath));
        if (newestFile != null)
        {
            DateTime currunt = DateTime.Now;
            DateTime old = newestFile.LastAccessTime;
            System.TimeSpan t = currunt.Subtract(old);
            double lastmodifiedDate = t.TotalMilliseconds;
            if (lastmodifiedDate <= 604800000)
            {
                Console.WriteLine("The File " + newestFile.Name + " has been used at " + newestFile.LastAccessTime.ToLocalTime());
            }
        }

 }
private FileInfo GetNewestFile(DirectoryInfo directoryInfo)
{
   var myFile = (from f in directoryInfo.GetFiles()
                          orderby f.LastWriteTime descending
                          select f).First();

   return new FileInfo(myFile.FullName);
}

Since the file has been used last week it also means it has been used last month.

Alyafey
  • 1,455
  • 3
  • 15
  • 23