Rob*_*Day 12
像这样......
FileInfo fileInfo = new FileInfo(@"C:\MyFile.txt"));
bool myCheck = fileinfo.CreationTime > DateTime.Now.AddHours(-23);
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 11
使用:
System.IO.File.GetCreationTime(filename);
Run Code Online (Sandbox Code Playgroud)
要获取文件的创建时间,请参阅GetCreationTime以获取更多详细信息和示例.
然后你可以这样做:
public bool IsBelowThreshold(string filename, int hours)
{
var threshold = DateTime.Now.AddHours(-hours);
return System.IO.File.GetCreationTime(filename) <= threshold;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用File.GetCreationTime和比较当前时间:
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));
Run Code Online (Sandbox Code Playgroud)