0

In my Downloading document Application I am getting the Stackoverflow Exception as Unhandled when I am iterating through the Directory to get the files details and renaming & moving the files to some folder my code is

public FileInfo GetNewestFile()
{
    try
    {
        System.IO.DirectoryInfo directory = new DirectoryInfo(TempDownloadFolder);

        FileInfo result = null;
        var list = directory.GetFiles(); // Stackoverflow Exception occurs here
        if (list.Count() > 0)
        {
            result = list.OrderByDescending(f => f.LastWriteTime).First();
        }
        return result;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}            

i.e The application downloads the PDF and MS-Word Files from a website if it downloads PDF file sequentially the directory.GetFiles() works fine, but when it downloads 1 or more PDF file and then downloads a MS-Word file the application throws System.Stackoverflow Exception.

When I restart the application downloads the MS-Word file as its is the first file in the lineup it works well only until another MS-Word` file comes up for download after few files have been downloaded

As far as my knowledge the Exception may be occurring because of huge memory allocated but I cannot figure out why its not happening for PDF file but only happening for MS-Word file

Edit:

The previous code I had used to return newest file was

return di.GetFiles()
       .Union(di.GetDirectories().Select(d => GetNewestFile()))
       .OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
       .FirstOrDefault(); 

the above code also resulted in Stackoverflow exception

Rajesh
  • 1,600
  • 5
  • 33
  • 59
  • Is there other app/method continuously writing to TempDownloadFolder? – Martheen Oct 17 '16 at 07:42
  • @Martheen Yes the file will be countinously downloded to the `TempDownloadFolder` but after renaming it will be moved to the Permanent Download folder – Rajesh Oct 17 '16 at 08:00
  • The PeterB answer combined with his comment on using simple loop to get the latest file might work by avoiding the newer file during the call and only keeping at max two file properties on memory at once – Martheen Oct 17 '16 at 08:06

2 Answers2

2

Please try directory.EnumerateFiles() instead of directory.GetFiles(). Then also, instead of .Count() > 0 use .Any().

They differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.
  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

From this MSDN page: https://msdn.microsoft.com/en-us/library/4cyf24ss(v=vs.110).aspx

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • With OrderByDescending, won't it need to enumerate the whole collection anyway? – Martheen Oct 17 '16 at 07:47
  • 1
    That's quite possible indeed, I did not notice that call. But that could quite easily be rewritten using a loop to look for & keep the newest file, without keeping all files in memory at once (which is what happens in the original code). – Peter B Oct 17 '16 at 07:51
  • Incidentally, [EnumerateFiles](http://stackoverflow.com/questions/29555761/what-happens-with-directory-enumeratefiles-is-directory-content-changes-during-i) skip deleted files and ignore new files if any changes happen during iteration. Later comment from OP mention files are continuously downloaded, so EnumerateFiles might be the perfect solution by ignoring the continuously added files and avoid the stackoverflow exception – Martheen Oct 17 '16 at 08:03
-1

you should check your TempDownloadFolder string value. For me it is working fine.

Create a Folder Name Temp in bin/debug/Temp project directory

  public FileInfo GetNewestFile()
    {
        try
        {
            System.IO.DirectoryInfo directory = new DirectoryInfo(@"Temp");

            FileInfo result = null;
            var list = directory.GetFiles(); // Stackoverflow Exception occurs here
            if (list.Count() > 0)
            {
                result = list.OrderByDescending(f => f.LastWriteTime).First();
            }
            return result;
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }