c# - 按创建顺序获取文件列表

标签 c#

我当前正在将不同目录中的文件列表读入列表中:

public List<string> MapMyFiles()
        {
            List<string> batchaddresses = new List<string>();
            foreach (object o in lstViewAddresses.Items)
            {
                try
                {
                    batchaddresses.AddRange(Directory.GetFiles(o.ToString(), "*-E.esy"));
                }
                catch
                {
                    if (MessageBox.Show(o.ToString() + " does not exist. Process anyway?", "Continue?", MessageBoxButtons.YesNo)
                        == DialogResult.Yes) { }
                    else
                    {
                        Application.Exit();
                    }
                }

            }

我如何在列表中按创建日期对它们进行排序?

最佳答案

尝试使用 FileInfo 对象并按 CreationTime 属性排序,然后将其作为 FullName (即路径)返回

public List<string> MapMyFiles()
{
    List<FileInfo> batchaddresses = new List<FileInfo>();
    foreach (object o in lstViewAddresses.Items)
    {
        try
        {
            string[] files = Directory.GetFiles(o.ToString(), "*-E.esy");
            files.ToList().ForEach(f => batchaddresses.Add(new FileInfo(f)));
        }
        catch { }
    }

    return batchaddresses.OrderBy(f => f.CreationTime).Select(f => f.FullName).ToList();
}

关于c# - 按创建顺序获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4502456/

相关文章:

c# - MySql数据库连接与C#同步以查看特定表值

c# - 是否存在用于生成 WCF 服务以为 self 跟踪实体提供获取和保存的 T4 模板?

c# - 错误的 ClientCredentials 的常见做法是什么?

c# - 如何检查我的多线程代码是否确实在多个线程上运行?

c# - Unity2D - 在网格中移动 2D 对象,如定时时尚

c# - ElasticSearch 与 SQL 全文搜索

c# - 执行阅读器 : Connection property has not been initialized

c# - ASP.NET MVC - 找到与 Controller 匹配的多种类型

c# - 单元测试 VS 2013 - list 引用文件 'vstest.executionengine.appcontainer.exe',它不是有效负载的一部分

c# - RabbitMQ 可以告诉我一条消息已传递了多少次吗?