17

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

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

但是,如果有很多创建时间不同的文件,我如何(什么模式)只获取创建时间今天的文件?

谢谢!

4

7 回答 7

28

试试这个:

var todayFiles = Directory.GetFiles("path_to_directory")
                 .Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date);
于 2012-12-13T18:05:08.817 回答
28

为了性能,特别是如果目录搜索可能很大,使用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()确保数据正确。

于 2012-12-13T19:32:09.390 回答
3

您需要获取文件的目录信息

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;
}
于 2012-12-13T18:07:54.727 回答
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();

请参见此处:如何使用 .NET 在目录中查找最新文件且不循环?

于 2012-12-13T18:03:33.393 回答
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;
于 2012-12-13T18:04:06.937 回答
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
}
于 2012-12-13T18:09:50.153 回答
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(); 
于 2018-04-25T14:58:18.827 回答