jac*_*obz -1 c# arrays listview
所以我在一个目录中得到了一堆文件.
它们都是这样命名的:
所以我将所有这些项目放在一个目录中并将它们放入一个数组中:
string[] pictures = Directory.GetFiles(@"C:/Pictures", "*.jpg");
Run Code Online (Sandbox Code Playgroud)
我得到了一个包含3列的ListView,名称,日期和文件大小.
我想从文件名中获取所有这些信息,然后将它们放入listview.所以对于这三个文件,它看起来像这样:
名称--------------日期---------------------------文件大小
丹尼尔---- --------- 10.2013年9月-------- 26 KB
彼得-------------- 06.2012年5月----------------- 39 KB
Christiane -------- 08.2011年1月------------ 35 KB
所以我想到了在数组中拆分信息foreach然后使用另一个循环来编写ListView中的数据,但我不确切知道如何做到这一点.
任何帮助表示赞赏^^
干杯
我感觉过于慷慨......通常我会说自己开始吧...但这在睡觉前扔在一起似乎很有趣.
class PictureLoader {
private readonly string[] _images;
public PictureLoader(string path) {
_images = Directory.GetFiles(path, "*.jpg");
}
public IEnumerable<Tuple<string, string, string>> GetRowData() {
foreach (var imagePath in _images) {
var extension = Path.GetExtension(imagePath);
var fileName = Path.GetFileName(imagePath);
var regex = Regex.Match(fileName, @"([A-Za-z]+)-(\d{4}-\d{2}-\d{2})(.[A-Za-z]+)");
var name = regex.Groups[1].Value;
var date = DateTime.ParseExact(regex.Groups[2].Value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
yield return
new Tuple<string, string, string>(name + extension, date.ToString(),
(new FileInfo(imagePath).Length / 1024).ToString() + " KB");
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var pictureLoader = new PictureLoader(@"folder here");
foreach (var group in pictureLoader.GetRowData()) {
var item = new ListViewItem();
item.Text = group.Item1;
item.SubItems.Add(group.Item2);
item.SubItems.Add(group.Item3);
listView1.Items.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
结果是:

这是你的起点.我会留下我错过的细节给你.
| 归档时间: |
|
| 查看次数: |
11802 次 |
| 最近记录: |