Question: At the moment I have my code to get some files from a Dir.

```
foreach (var file in
            Directory.GetFiles(MainForm.DIRECTORY_PATH, "*.csv"))
            {
                //Process File
                string[] values = File.ReadAllLines(file)
                .SelectMany(lineRead => lineRead.Split(',')
                .Select(s => s.Trim()))
                .ToArray();

```

I want to be able to order these file by date order first before i start reading them and processing them.

I looked at a suggestion on MDSN to use DirectoryInfo:

```
        DirectoryInfo DirInfo = new DirectoryInfo(MainForm.DIRECTORY_PATH);
        var filesInOrder = from f in DirInfo.EnumerateFiles()
                           orderby f.CreationTime
                           select f;

        foreach (var item in filesInOrder)
        {
             //Process File
                string[] values = File.ReadAllLines(item )
                .SelectMany(lineRead => lineRead.Split(',')
                .Select(s => s.Trim()))
                .ToArray();
        }

```

this doesnt work however as the System.IO.File.ReadAllLine(file) seems to red line with the error as item is a string and not an actual file. :(

Does anyone know a solution to this or has had a similar issue? :)

Regards
J.

Answer: 
From [**MSDN**](http://msdn.microsoft.com/en-us/library/s2tte0y1%28v=vs.110%29.aspx) `File.ReadAllLines(string path)` takes file path as input.

> 
> 
> > 
> > Opens a text file, reads all lines of the file, and then closes the file.
> > 
> > 
> > 
> 
> 
> 

You have to pass file path:

```
string[] values = File.ReadAllLines(item.FullName)

```

your code:

```
foreach (var item in filesInOrder)
{

  string[] values = File.ReadAllLines(item.FullName)
                    ...............................
                    ...............................

}

```

You can replace all of your chunk with following code via lambda expressions:

```
var values = DirInfo.EnumerateFiles().OrderBy(f => f.CreationTime)
                    .Select(x => File.ReadAllLines(x.FullName)
                                 .SelectMany(lineRead => lineRead.Split(',')
                                 .Select(s => s.Trim())).ToArray()
                           );

```
Your first code snippet reads all lines in **one** file, where as the second one reads from all files in the directory. So it is not very clear what you want to do.

The second code snippet cannot work, because the variable `values` is declared inside the loop. Its visibility scope is limited to the code block of the loop. The result will therefore never be visible outside of the loop.

```
var filesInOrder = from f in DirInfo.EnumerateFiles() ...;
var items = new List<string>();
foreach (FileInfo f in filesInOrder) {
    using (StreamReader sr = f.OpenText()) {
        while (!sr.EndOfStream) {
            items.AddRange(sr.ReadLine().Split(','));
        }
    }
}

```

Here I define a `List<string>` before the loop that will hold all the items of all files. We need two loops: one that loops over the files (`foreach`) and one that reads the lines in each file and successively adds items to the list (`while`).