0

I'd like to continuously read in all image files from a folder on my local drive, then do some processing and end the program once all images have been read. The images numbers are not sequential they are random however they are located in a single folder. Currently my program can only read in one file; see code below

string imagePath = Path.Combine(Freconfig.GetSamplesFolder(), "24917324.jpg");
Sinatr
  • 20,892
  • 15
  • 90
  • 319
JpersaudCodezit
  • 143
  • 2
  • 13

4 Answers4

5

use FileSystemWatcher https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = Freconfig.GetSamplesFolder();
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.jpg";

    // Add event handlers.
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Deleted += new FileSystemEventHandler(OnChanged);
    watcher.Renamed += new RenamedEventHandler(OnRenamed);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

before starting watcher, use directorylisting to find all existing files and process them, then use watcher

Ive
  • 1,321
  • 2
  • 17
  • 25
  • Im not sure, what do you mean by use directorylisting to find all existing files? Would it be best to do my processing before I start the watcher? – JpersaudCodezit Jul 13 '16 at 15:11
  • When you only need to proccess all jpg in Directory then use what Mfusiki suggest. – Ive Jul 13 '16 at 17:05
1

Is this what you are looking for? Directory.GetFiles(@"..\somepath") (MSDN)

Michael
  • 3,350
  • 2
  • 21
  • 35
1

You could try:

directoryInfo = new DirectoryInfo("C:/YOUR/DIRECTORY/HERE]");
var files = directoryInfo.GetFiles("*.jpg").OrderBy(x => x.CreationTimeUtc);
foreach (var file in files)
{
 //Your processing
}

Note this will get all the .jpg files in a directory. The foreach loop will start with the oldest files first.

Riaan van Zyl
  • 538
  • 8
  • 17
1

This should get all the files in a directory:

 private List<FileInfo> GetFileInfo()
    {
        string path = @"C:\MyPath";
        List<FileInfo> files = new List<FileInfo>();
        DirectoryInfo di = new DirectoryInfo(path);

       //TopDirectoryOnly if you don't want subfolders
        foreach (FileInfo f in di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly))
        {
            files.Add(f);
        }

        return files;
    }

Then in your code, iterate over the returned collection and do whatever work you need to do with them.

Matthew Alltop
  • 501
  • 4
  • 20