[ACCEPTED]-Check If File Created Within Last X Hours-c#
Accepted answer
Something like this...
FileInfo fileInfo = new FileInfo(@"C:\MyFile.txt"));
bool myCheck = fileinfo.CreationTime > DateTime.Now.AddHours(-23);
0
Use:
System.IO.File.GetCreationTime(filename);
To get the creation time of the file, see 2 GetCreationTime for more details and examples.
Then you 1 can do something like:
public bool IsBelowThreshold(string filename, int hours)
{
var threshold = DateTime.Now.AddHours(-hours);
return System.IO.File.GetCreationTime(filename) <= threshold;
}
You can use File.GetCreationTime
and compare to the current 1 time:
private static bool IsFileOlder(string fileName, TimeSpan thresholdAge)
{
return (DateTime.Now - File.GetCreationTime(fileName)) > thresholdAge;
}
// used like so:
// check if file is older than 23 hours
bool oldEnough = IsFileOlder(@"C:\path\file.ext", new TimeSpan(0, 23, 0, 0));
// check if file is older than 23 milliseconds
bool oldEnough = IsFileOlder(@"C:\path\file.ext", new TimeSpan(0, 0, 0, 0, 23));
Use FileInfo
class and the CreationTime
property.
FileInfo fi = new FileInfo(@"C:\myfile.txt");
bool check = (DateTime.Now - fi.CreationTime).TotalHours < 23;
0
FileInfo fi = new FileInfo("c:\\file.txt");
if (fi.CreationTime.AddHours(23) >= DateTime.Now)
{
//created within the last 23 hours
}
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.