[英]How do i add to the List<FileInfo> also the size of each file as a text?
files = new DirectoryInfo(@"d:\").GetFiles("*.avi").Where(f => f.Length <= 70 * 1024 * 1024).ToList();
For example the first item I see it now in the List like:例如,我现在在 List 中看到的第一个项目,如:
{test.avi}
How can I make that I will see also the size of each file, something like:我怎样才能让我也看到每个文件的大小,比如:
{test.avi} {23MB}
First of all, files
will be a List<FileInfo>
, not a List<string>
.首先, files
将是List<FileInfo>
,而不是List<string>
。 To convert the FileInfo
into a string
of your own choice, use Select
:要将FileInfo
转换为您自己选择的string
,请使用Select
:
files = new DirectoryInfo(@"d:\").GetFiles("*.avi").Where(f => f.Length <= 70 * 1024 * 1024).Select(f => f.Name + " " + (f.Length / 1048576) + " MB").ToList();
If you need more options (say, kB) the code inside the Select
will be more complicated.如果您需要更多选项(例如 kB),则Select
的代码会更复杂。 An often forgotten feature of LINQ is that you can use complete delegates inside LINQ methods: LINQ 的一个经常被遗忘的功能是您可以在 LINQ 方法中使用完整的委托:
.Select((f) => {
var s = f.Name + " ";
if (f.Length > 1048576) {
return s + (f.Length / 1048576) + " MB";
} else {
return s + (f.Length / 1024) + " kB";
}
})
files = new DirectoryInfo(@"d:\").GetFiles("*.avi").Where(f => f.Length <= 70 * 1024 * 1024).Select(f=> new {f.Name,f.Length})).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.