Directory.GetFiles get today's files only

前端 未结 7 644
青春惊慌失措
青春惊慌失措 2021-01-04 01:02

There is nice function in .NET Directory.GetFiles, it\'s simple to use it when I need to get all files from directory.

Directory.GetFiles(\"c:\\\\Files\")
<         


        
相关标签:
7条回答
  • 2021-01-04 01:48
    var directory = new DirectoryInfo(Path.GetDirectoryName(@"--DIR Path--"));
    DateTime from_date = DateTime.Now.AddDays(-5);
    DateTime to_date = DateTime.Now.AddDays(5);
    
    //For Today 
    var filesLst = directory.GetFiles().AsEnumerable()
                  .Where(file.CreationTime.Date == DateTime.Now.Date ).ToArray(); 
    
    //For date range + specific file extension 
    var filesLst = directory.GetFiles().AsEnumerable()
                  .Where(file => file.CreationTime.Date >= from_date.Date && file.CreationTime.Date <= to_date.Date && file.Extension == ".txt").ToArray(); 
    
    //To get ReadOnly files from directory  
    var filesLst = directory.GetFiles().AsEnumerable()
                  .Where(file => file.IsReadOnly == true).ToArray(); 
    
    //To get files based on it's size
    int fileSizeInKB = 100; 
    var filesLst = directory.GetFiles().AsEnumerable()
                  .Where(file => (file.Length)/1024 > fileSizeInKB).ToArray(); 
    
    0 讨论(0)
提交回复
热议问题