简体   繁体   English

如何在asp:Gridview中按日期对文件和文件夹进行排序?

[英]How do I sort files and folders by date in my asp:Gridview?

I have an application that on load displays a list of files, and date modified on my server in alphabetical order according to the file name 我有一个应用程序,该应用程序在加载时显示文件列表,并根据文件名的字母顺序在服务器上修改日期

DirectoryInfo di = new DirectoryInfo(Server.MapPath(strDirectory));
List<FileInfo> files = di.GetFiles().ToList();

How can I sort this by date modified? 如何按修改日期排序?

Use FileSystemInfo.LastWriteTime 使用FileSystemInfo.LastWriteTime

List<FileInfo> files = di.EnumerateFiles()
            .OrderBy(f => f.LastWriteTime)
            .ToList();

Here is another option: (do it all at once) 这是另一种选择:(一次完成所有操作)

List<FileInfo> files = new DirectoryInfo(Server.MapPath(strDirectory)).GetFiles()
                    .OrderByDescending(f => f.LastWriteTime)
                    .Select(f => f.Name)
                    .ToList();

Tips: you can type a dot after each extension method to explore more options for future references. 提示:您可以在每种扩展方法后键入点,以探索更多选项以供将来参考。 (ie instead of OrderByDescending(), you can use OrderBy(); you can just do a .ToList() after ordering without doing .Select(f => f.Name) if you like) (即,可以使用OrderBy()代替OrderByDescending();如果愿意,可以在订购后仅执行.ToList()而不执行.Select(f => f.Name))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM