Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Checking file longer than 1 hour

Checking file longer than 1 hour

Scheduled Pinned Locked Moved C#
helpdatabasedata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mninawa
    wrote on last edited by
    #1

    Hello Everyone I need someone who can help me.I want to check the oldest file in the directory, and if that file is older than one hour then send an alert. Here is my current work.

           for (int index = 0; index < files.Length; index++)//Loop through the file.
            {
                string lastMTime = File.GetLastAccessTime(files\[index\]).ToString("ddMMyyymmss");//Get the last modified file by date.
                files\[index\] = lastMTime + files\[index\];//Shuffle the array
            }
            Array.Sort(files);//Sort the array
            string oldFile = Path.GetFileName(files\[0\].Substring(15));//Get the old file.
            if(File.GetCreationTime(oldFile))//  this is where i am stuck.....
    

    if(File.GetCreationTime(oldFile))// this is where i am stuck..... Please my code project peeps, help me out.... Thank you all in Advance Marvel...

    M M L 3 Replies Last reply
    0
    • M Mninawa

      Hello Everyone I need someone who can help me.I want to check the oldest file in the directory, and if that file is older than one hour then send an alert. Here is my current work.

             for (int index = 0; index < files.Length; index++)//Loop through the file.
              {
                  string lastMTime = File.GetLastAccessTime(files\[index\]).ToString("ddMMyyymmss");//Get the last modified file by date.
                  files\[index\] = lastMTime + files\[index\];//Shuffle the array
              }
              Array.Sort(files);//Sort the array
              string oldFile = Path.GetFileName(files\[0\].Substring(15));//Get the old file.
              if(File.GetCreationTime(oldFile))//  this is where i am stuck.....
      

      if(File.GetCreationTime(oldFile))// this is where i am stuck..... Please my code project peeps, help me out.... Thank you all in Advance Marvel...

      M Offline
      M Offline
      Moreno Airoldi
      wrote on last edited by
      #2

      TimeSpan ts = DateTime.Now.Subtract(File.GetCreationTime(oldFile));
      if (ts.TotalHours >= 1) ...

      This should do the trick. :) Note that you are calling GetLastAccessTime() for sorting and GetCreationTime() for checking the file age, I'm not sure this is what you want.

      2+2=5 for very large amounts of 2 (always loved that one hehe!)

      1 Reply Last reply
      0
      • M Mninawa

        Hello Everyone I need someone who can help me.I want to check the oldest file in the directory, and if that file is older than one hour then send an alert. Here is my current work.

               for (int index = 0; index < files.Length; index++)//Loop through the file.
                {
                    string lastMTime = File.GetLastAccessTime(files\[index\]).ToString("ddMMyyymmss");//Get the last modified file by date.
                    files\[index\] = lastMTime + files\[index\];//Shuffle the array
                }
                Array.Sort(files);//Sort the array
                string oldFile = Path.GetFileName(files\[0\].Substring(15));//Get the old file.
                if(File.GetCreationTime(oldFile))//  this is where i am stuck.....
        

        if(File.GetCreationTime(oldFile))// this is where i am stuck..... Please my code project peeps, help me out.... Thank you all in Advance Marvel...

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        For date and time handling you must use the builtin DateTime type, it gives you all the methods you need. You can get the current time with DateTime.Now, and you can compare two date times or subtract one to another to obtain a TimeSpan value representing the difference between the two (Moreno Airoldi sowed you how to do this). Also, you don't need all the string parsing you are doing, just use a SortedDictionary with last access time for key and file path for value.

        modified on Thursday, July 30, 2009 6:21 AM

        1 Reply Last reply
        0
        • M Mninawa

          Hello Everyone I need someone who can help me.I want to check the oldest file in the directory, and if that file is older than one hour then send an alert. Here is my current work.

                 for (int index = 0; index < files.Length; index++)//Loop through the file.
                  {
                      string lastMTime = File.GetLastAccessTime(files\[index\]).ToString("ddMMyyymmss");//Get the last modified file by date.
                      files\[index\] = lastMTime + files\[index\];//Shuffle the array
                  }
                  Array.Sort(files);//Sort the array
                  string oldFile = Path.GetFileName(files\[0\].Substring(15));//Get the old file.
                  if(File.GetCreationTime(oldFile))//  this is where i am stuck.....
          

          if(File.GetCreationTime(oldFile))// this is where i am stuck..... Please my code project peeps, help me out.... Thank you all in Advance Marvel...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, two comments: 1. File.GetLastAccessTime does not really work on most systems. By default Windows does not track file access as that may create a heavy load on the file system; you can enable it through the registry somehow but I would not recommend that. 2. IMO LastModificationTime is much more useful than CreationTime, since LastModificationTime is about the file content, whereas CreationTime is about the empty file. So when I now copy an old fle, it normally keeps its LastModificationTime but it gets its CreationTime set to now. BTW: The above could be different for each and every file system (NTFS, FAT, FAT32, ...) :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups