Rajesh
Rajesh

Reputation: 1620

DirectoryInfo.GetFiles throws unhandled StackOverflowException

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

Upvotes: 0

Views: 1306

Answers (2)

Pravin Kr. Mishra
Pravin Kr. Mishra

Reputation: 11

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;
        }
    }   

Upvotes: -1

Peter B
Peter B

Reputation: 24147

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

Upvotes: 2

Related Questions