Checking file longer than 1 hour
-
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... -
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...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 andGetCreationTime()
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!)
-
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...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
-
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...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.