user6664813 user6664813 - 5 months ago 32
C# Question

Cannot implicitly convert type 'System.IO.FileInfo' to 'System.IO.FileInfo[]

I'm getting an error in the

clause in the below code. The error states "Cannot implicitly convert type 'System.IO.FileInfo' to 'System.IO.FileInfo[]'

What I'm trying to accomplish is to get every file in the directory after or between a specific date. Then I need to loop through each file and get some information about the file.

DirectoryInfo info = new DirectoryInfo(dirtxt.Text);
FileInfo[] fls = info.GetFiles()
.Where(file=>file.LastWriteTime >= DateTime.Now).First();

foreach (FileInfo fi in fls)
//Collect file information here


You are using First() so this should be :

FileInfo fls = info.GetFiles()
                .Where(file => file.LastWriteTime >= DateTime.Now).First();

or with out using First()

FileInfo[] fls = info.GetFiles()
                    .Where(file => file.LastWriteTime >= DateTime.Now).ToArray();

or my preferred method dont specify a type and use IEnumerable

var fls = info.GetFiles().Where(file => file.LastWriteTime >= DateTime.Now);