Directory.GetFiles仅获取今天的文件

时间:2012-12-13 18:01:20

标签: c# asp.net .net

.NET Directory.GetFiles中有很好的功能,当我需要从目录中获取所有文件时,使用它很简单。

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

但是如果有很多创建时间不同的文件,我怎么用(什么模式)才能获得今天创建时间的文件呢?

谢谢!

7 个答案:

答案 0 :(得分:25)

为了提高性能,特别是如果目录搜索可能很大,使用Directory.EnumerateFiles(),懒惰地在搜索路径上进行枚举,优于Directory.GetFiles(),它急切地通过搜索路径进行枚举,在过滤任何匹配之前收集所有匹配项:

DateTime today = DateTime.Now.Date ;
FileInfo[] todaysFiles = new DirectoryInfo(@"c:\foo\bar")
                         .EnumerateFiles()
                         .Select( x => {
                            x.Refresh();
                            return x;
                         })
                         .Where( x => x.CreationTime.Date == today || x.LastWriteTime == today )
                         .ToArray()
                         ;

请注意,FileSystemInfo及其子类型的属性可以(并且)被缓存,因此它们不一定反映当前的当前现实。因此,调用Refresh()以确保数据正确。

答案 1 :(得分:24)

试试这个:

var todayFiles = Directory.GetFiles("path_to_directory")
                 .Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date);

答案 2 :(得分:2)

您需要获取文件的directoryinfo

public List<String> getTodaysFiles(String folderPath)
{
    List<String> todaysFiles = new List<String>();
    foreach (String file in Directory.GetFiles(folderPath))
    {
        DirectoryInfo di = new DirectoryInfo(file);
        if (di.CreationTime.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            todaysFiles.Add(file);
    }
    return todaysFiles;
}

答案 3 :(得分:1)

您可以使用此代码:

var directory = new DirectoryInfo("C:\\MyDirectory");
var myFile = (from f in directory.GetFiles()
             orderby f.LastWriteTime descending
             select f).First();

// or...
var myFile = directory.GetFiles()
             .OrderByDescending(f => f.LastWriteTime)
             .First();

见这里:How to find the most recent file in a directory using .NET, and without looping?

答案 4 :(得分:0)

使用System.Linq;

DirectoryInfo info = new DirectoryInfo("");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
    // DO Something...
}

如果您想将其分解为特定日期,可以尝试使用过滤器

var files = from c in directoryInfo.GetFiles() 
            where c.CreationTime >dateFilter
            select c;

答案 5 :(得分:0)

你应该能够解决这个问题:

var loc = new DirectoryInfo("C:\\");


var fileList = loc.GetFiles().Where(x => x.CreationTime.ToString("dd/MM/yyyy") == currentDate);
foreach (FileInfo fileItem in fileList)
{
    //Process the file
}

答案 6 :(得分:0)

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();