简体   繁体   English

我如何添加到列表中<FileInfo>还有每个文件的大小作为文本?

[英]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.

相关问题 如何制作嵌套的FileInfo列表? - How do i make a nested FileInfo List? 如何在FileInfo中获取文件大小? - How to get Size of file in FileInfo? 如何使用DirectoryInfo和FileInfo获取所有文件并将排序后的文件添加到列表中? - How do i use DirectoryInfo and FileInfo to get all files and add the sorted files to a List? 如何对FileInfo []中的文件进行排序,然后将硬盘中的最后一个Gif文件从FileInfo []分配给pictureBox1? - How do i sort the files in FileInfo[] and then assign the last Gif file from the hard disk from FileInfo[] to pictureBox1? 我如何添加到列表 <string> 还有文件目录,然后从列表中获取单个字符串var仅文件名和目录? - How do i add to the List<string> also the files directory and then getting to a single string var only the file name and dir from the List? 如何制作清单清单? 然后添加到每个List值? - How do I make List of Lists? And then add to each List values? 如何将列表保存到文本文件? - How do I save list to text file? FileInfo从列表中删除文件 - FileInfo remove file from list 如何在列表视图中的文件资源管理器中区分 FileInfo 和 DirectoryInfo - How to make difference between FileInfo and DirectoryInfo in a file explorer in list view 当文件的内容也带有希伯来字母时,如何使用openFileDialog打开文本文件? - How do i open a text file with openFileDialog when the content of the file is also with hebrew letters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM