Given your concern with large number of files in a given folder, and desire to load them on demand, I'd recommend the following approach -
(Note - The suggestion of calling Directory.Enumerate().Skip...
in the other answer works, but is not efficient, specially so for directories with large number of files, and few other reasons)
// Local field to store the files enumerator;
IEnumerator<string> filesEnumerator;
// You would want to make this call, at appropriate time in your code.
filesEnumerator = Directory.EnumerateFiles(folderPath).GetEnumerator();
// You can wrap the calls to MoveNext, and Current property in a simple wrapper method..
// Can also add your error handling here.
public static string GetNextFile()
{
if (filesEnumerator != null && filesEnumerator.MoveNext())
{
return filesEnumerator.Current;
}
// You can choose to throw exception if you like..
// How you handle things like this, is up to you.
return null;
}
// Call GetNextFile() whenever you user clicks the next button on your UI.
Edit: Previous files can be tracked in a linked list, as the user moves to next file.
The logic will essentially look like this -
- Use the linked list for your previous and next navigation.
- On initial load or click of
Next
, if the linked list, or its next node is null, then use the GetNextFile
method above, to find the next path, display on UI, and add it to the linked list.
- For
Previous
use the linked list to identify the previous path.