C# 如何判断该文件是否在过去一周和过去一个月内被使用?

C# 如何判断该文件是否在过去一周和过去一个月内被使用?,c#,windows,C#,Windows,我使用下面的代码从目录中获取最新修改的文件: String tmpPath="C:\demotestDirectory"; FileInfo newestFile = GetNewestFile(new DirectoryInfo(tmpPath)); if (newestFile != null) { DateTime lastmodifiedDate = newestFile.LastAccessTime; string currentMonth = DateTime.Now.M

我使用下面的代码从目录中获取最新修改的文件:

String tmpPath="C:\demotestDirectory";
FileInfo newestFile = GetNewestFile(new DirectoryInfo(tmpPath));
if (newestFile != null)
{
   DateTime lastmodifiedDate = newestFile.LastAccessTime;
   string currentMonth = DateTime.Now.Month.ToString();
}
我从目录中得到了最新修改的文件,现在我想告诉你,在过去的一周里,以及在过去的一个月里,这个文件是否被使用过


感谢您的帮助。

使用此功能查找7天前的:

DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));
请注意,您没有得到上次修改。你最后一次被访问。上次修改时间使用:

或者:根据您的评论,类似于:

DateTime.Now.Subtract(new TimeSpan((int)DateTime.Now.DayOfWeek, 0, 0, 0));
此代码首先检查文件是否在同一年编辑。它每月检查一次。然后,它将calendar类与当前的DateTimeInfo一起使用(其中包括:一周有多少天,这是一周的第一天,等等)。函数GetWeekOfYear返回星期数。比较这两个整数,然后开始

注:

您使用了LastAccessTime,但当您在资源管理器中单击文件之类的小操作时,它也会更新(因此,如果您想知道用户是否真的打开了它,这不是很有帮助)。改为使用LastWriteTime(如果文件已更改,则会更改)。

您可以执行以下操作:

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);
}
private void fileUsage()
{
字符串tmpPath=“C:\\demotestDirectory”;
FileInfo newestFile=GetNewestFile(newdirectoryinfo(tmpPath));
if(newestFile!=null)
{
DateTime currunt=DateTime.Now;
DateTime old=newestFile.LastAccessTime;
System.TimeSpan t=当前减法(旧);
double lastmodifiedDate=t.Total毫秒;

如果(lastmodifiedDate比较两个DateTime对象…我可以通过获取当前月份-1并与我拥有的日期进行比较来获取月份部分,我在执行上周部分时遇到问题。关于
LastAccessTime
,如果您在Windows注册表中有
NtfsDisableLastAccessUpdate=1
,则不会更新
LastAccessTime
。will将其更改为newest file.LastWriteTime;help?这需要操作系统的帮助,这是一种叫做审计的功能。一篇KB文章描述了如何打开它。我不认为声音写为7天不是我想要的,它是过去的一周和过去的一个月!因此我不能使用7天或30天。有什么区别吗?会持续时间吗IFED不会与上次访问的日期相同?@confusedMind为什么这与您在评论中写的减去30天的月份不同?@confusedMind否。如果它已阅读但未更改,则不会被修改。因为假设日期为2012年10月25日,最后30天=可能为2012年9月25日。并且该文件在2012年9月5日被修改,因此仍然超过了一个月n但不会处于这种状态。
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);
}