C#: Reading the next file in a folder?
<p>I have a folder containing <code>.txt</code> files which are numbered like so:</p>

<pre><code>0.txt
1.txt
...
867.txt
...
</code></pre>

<p>What I am trying to do is each time <code>readNextFile();</code> is called, I want it to return the contents of the next file in that folder, and <code>return string.Empty;</code> if there is no file after the last one it read. <em>I want to have a button that, when pressed, will make the program read the next file and do stuff with it's contents. The files might change between button presses.</em> The way I did this before was this:</p>

<pre><code>int lastFileNumber = 0;

string readNextFile()
{
    string result = string.Empty;
    //I know it is recommended to use as few of these as possible, this is just an example.
    try
    {
        string file = Path.Combine("C:\Somewhere", lastFileNumber.ToString() + ".txt");
        if (File.Exists(file))
        {
            result = File.ReadAllText(file);
            lastFileNumber++;
        }
    }
    catch
    {

    }
    return result;
}
</code></pre>

<p>Problem is there might sometimes be this kind of situation:</p>

<pre><code>0.txt
1.txt
5.txt
6.txt
...
</code></pre>

<p>It would obviously get stuck at <code>1.txt</code> because <code>2.txt</code> doesn't exist. I need it to skip to the next existing file and read that one. And clearly it is <strong>not possible to just sort the file names alphabetically</strong> in a string array since the file names are not Padded, so doing that will result in <code>1000000000.txt</code> being read right after <code>1.txt</code>.</p>

<p>Any idea how I can achieve this?</p>