locked
find if new files exist in the folder path RRS feed

  • Question

  • User-1660589204 posted

    I have a folder path and my case is to check if there are new files( based on last 5 minutes) exist in that folder. Please let me know what to do to my existing code:

    public FilePickerService(IConfiguration config)
    {
       path = config["FilePath:SharedFolderPath"];
       if (!File.Exists(path))
       {
    
       }
    }

    I need to check if new file exists and take that new file to pass to an API. Please help me how to do this. The files that I receive will be media files. 

    Friday, November 6, 2020 10:35 AM

All replies

  • User303363814 posted

    I think you already have an answer from another question but since you seem to have some sort of configuration object I would suggest that you move the magic '5' into the config file.

    var ageMinutes = int.Parse(config["File:Recent"]);
    var recent = DateTime.Now.AddMinutes(-1 * ageMinutes);
    var recentFileNames = Directory.EnumerateFiles(config["File:SharedFolderPath"])
                                   .Where(file => File.GetCreationTIme(file) > recent);

    You now have a lazy evaluated enumerable of all the recently created files where the definition of 'recent' can be changed via configuration as well as the holding directory name.

    Friday, November 6, 2020 11:22 PM
  • User409696431 posted
    File.GetCreationTIme

    should be

    File.GetCreationTime"

    Just a typo.

    Saturday, November 7, 2020 12:13 AM