c# - ListBox 按文件创建时间排序
问题描述
我的 listBox1 充满了 txt 文件的名称。它按字母顺序自动对它们进行排序。但我需要按创建时间对这些文件进行排序,从最新到最旧。有人能帮我吗?
解决方案
你没有给我们太多的继续:P
但我会尽我最大的努力来帮助你。因为我不知道您是如何填充列表的(手动或使用代码),所以我无法真正指导您。
但是,假设您是通过代码完成的。我输入的以下代码段可能会帮助您找到所需的内容
//array of file names (these need to be paths to files, so if you do not prefix it with ./path/ or anything, then the executable needs to be in the same folder)
//If you are debugging, make sure the paths are correct if you expect them to be in the same folder, it should then be in the debug folder.
//If you included some test files in the solution and want them copied to your debug folder. Click on them, go to the properties pane, and set build action to content, and one of the other properties should read copy of newer instead of never copy
var fileNames = new string[]{"1.txt", "2.txt", "3.txt"};
var fileInfos = fileNames.Select(f => new FileInfo(f));
var orderedFileInfos = fileInfos.OrderBy(f => f.CreationTime);
orderedFileInfos 现在是一个 FileInfo 类型的列表,按创建时间升序排列。
这是你可以用来填充你的 ListBox
我希望我能帮助你在你的项目中取得进展。
推荐阅读
- c# - 如何制作使用 DbContext 保存数据的 TCP 服务器?
- ios - 按需重新加载 UITabBarController
- android - 如何指定 URL 查询参数深层链接清单
- python-3.x - Python使用字典键值从字符串中获取第一个和最后一个值
- node.js - 不推荐使用 Node JS:不推荐使用缓冲区。在 API 中连接到 SQL Server 时
- cdi - 一段时间后注入的变量为空
- bash - /bin/sh: -c: 第 1 行:语法错误:bash 中的文件意外结束
- r - 如何从对角线上删除变量名称并将它们放在 R 函数 scatterplotMatrix 的轴上?
- javascript - JS 代码不会在对 innerHTML 的 AJAX 响应中运行
- git - 是否可以自动创建新分支、将 repo 分叉到本地、替换子文件夹中所有文件中的字符串并创建拉取请求?