C# Directory.GetFiles 只获取今天的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:00:28  来源:igfitidea点击:

Directory.GetFiles get today's files only

c#asp.net.net

提问by ihorko

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

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

Directory.GetFiles("c:\Files")

But how (what pattern) can I use to get only files that created time have today if there are a lot of files with different created time?

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

Thanks!

谢谢!

采纳答案by kmatyaszek

Try this:

尝试这个:

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

回答by MUG4N

You could use this code:

您可以使用此代码:

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

see here: How to find the most recent file in a directory using .NET, and without looping?

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

回答by MethodMan

using System.Linq;

使用 System.Linq;

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

if you wanted to break it down to a specific date you could try this using a filter

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

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

回答by Sorceri

You need to get the directoryinfo for the file

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

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;
}

回答by Abdel Raoof

You should be able to get through this:

你应该能够通过这个:

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
}

回答by Nicholas Carey

For performance, especially if the directory search is likely to be large, the use of Directory.EnumerateFiles(), which lazily enumerates over the search path, is preferable to Directory.GetFiles(), which eagerly enumerates over the search path, collecting all matches before filtering any:

对于性能,特别是如果目录搜索可能很大,则使用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()
                         ;

Note that the the properties of FileSystemInfoand its subtypes can be (and are) cached, so they do not necessarily reflect current reality on the ground. Hence, the call to Refresh()to ensure the data is correct.

请注意,属性FileSystemInfo及其子类型可以(和)缓存,因此它们不一定反映当前的实际情况。因此,Refresh()需要确保数据正确。

回答by Access Denied

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