Directory.GetFiles get today's files only

前端 未结 7 643
青春惊慌失措
青春惊慌失措 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:34

    using 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;
    
    0 讨论(0)
  • 2021-01-04 01:35

    Try this:

    var todayFiles = Directory.GetFiles("path_to_directory")
                     .Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date);
    
    0 讨论(0)
  • 2021-01-04 01:41

    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;
    }
    
    0 讨论(0)
  • 2021-01-04 01:43

    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:

    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 FileSystemInfo and 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.

    0 讨论(0)
  • 2021-01-04 01:44

    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
    }
    
    0 讨论(0)
  • 2021-01-04 01:45

    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?

    0 讨论(0)
提交回复
热议问题