4

I have seen a really close example here:

Sorting Files by date

But I am new to LINQ and couldn't get it to work (not sure I understand the DirectoryInfo or FileInfo classes).

Here are the necessary code snippets:

(When assigning the array):

string[] files = Directory.GetFiles(Server.MapPath("~/User_Saves/" + WebSecurity.CurrentUserName), "*.xml");

for(int i = 0; i < files.Length; i++)
{
    files[i] = files[i].Substring(files[i].LastIndexOf("\\") + 1);
    files[i] = files[i].Substring(0, files[i].Length - 4);
}

(That last part, i.e., the 'for loop', simply strips the path to the file and the only expected file extension, which is ".xml", of course, from the string, leaving only the clean file name).

(When writing the array):

[this snippet may not be necessary for answering this question, but just in case]

@foreach(string file in files)
{
    <p>
        <button title="Permanently delete the requisition named, &quot;@file&quot" type="button" id="@file" class="fileDelBtn">DEL</button>&nbsp;<span style="color: #000;">~</span>&nbsp;<span id="@file" class="listFile">@file</span>
    </p>
    hasSavedFiles = true;
}

Things I have tried:

string [] files = new DirectoryInfo(Server.MapPath("~/User_Saves/" + WebSecurity.CurrentUserName)).GetFiles().OrderBy(files => files.LastWriteTime).ToArray;

Fails because of this error: CS0136: A local variable named 'files' cannot be declared in this scope because it would give a different meaning to 'files', which is already used in a 'parent or current' scope to denote something else

For one, I can't understand the lambda operator, for the life of me, even after looking here: http://msdn.microsoft.com/en-us/library/bb311046.aspx (clarification on this alone, would be greatly appreciated, but is not really the main question, by any means).

Secondly, using this example, I know that DirectoryInfo() has no overloads that take 2 arguments, so I may lose my ability to "Get" only "*.xml" files, which I would love to preserve, but is probably not absolutely necessary.

3
  • simply change it to .OrderBy(f => f.LastWriteTime) Commented Apr 15, 2013 at 16:08
  • @IliaG I will try that now. What is f, in this example? Commented Apr 15, 2013 at 16:09
  • f is the element in the files array Commented Apr 15, 2013 at 16:13

2 Answers 2

10
var filesInOrder = new DirectoryInfo(path).GetFiles()
                        .OrderByDescending(f => f.LastWriteTime)
                        .Select(f => f.Name)
                        .ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Just so I know what to expect, this will give me a list and not an array correct? (This is no problem, of course)
@VoidKing if you need array, then just use ToArray() instead of ToList()
Honestly, either way is fine with me, but is there a way to prevent it from adding files that don't end with ".xml" or removing files that don't end with ".xml"?
@VoidKing just insert .Where(f=>f.Extension==".xml") after OrderByDescending (or better .Where(f=>String.Compare(f.Extension,".xml",true)==0) )
0

Something like this will do if you don't want to enumerate the files right away.

public static IEnumerable<string> GetXmlFilesByLastWriteTime(string path)
{
    var directoryInfo = new DirectoryInfo(path);
    if(!directoryInfo.Exists) return Enumerable.Empty<string>();

    var query = 
        from file in directoryInfo.GetFiles()
        where file.Extension.ToLower() == ".xml"
        orderby file.LastWriteTime
        select file.Name;

    return query;
}

Usage:

var files = GetXmlFilesByLastWriteTime(path).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.