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