[英]C# Get file size of array with paths
I am new to arrays and I want to display the size (in MB) of multiple files into a textBox. 我是数组的新手,我想将多个文件的大小(以MB为单位)显示到textBox中。 The paths to the files are in an array.
文件的路径在一个数组中。
var Files = Directory.GetFiles(Path, "*" + filetype, SearchOption.AllDirectories);
I saw this code in another post to get the size of a file: 我在另一篇文章中看到了这段代码来获取文件的大小:
long length = new System.IO.FileInfo(file).Length;
How can I add all of the file sizes to an int/string and write them into the textBox? 如何将所有文件大小添加到int / string并将其写入textBox?
If i understand you correctly, just use Linq Select
and string.Join
如果我理解正确,只需使用Linq
Select
和string.Join
var results = Directory.GetFiles(Path, "*" + filetype, SearchOption.AllDirectories)
.Select(file => new FileInfo(file).Length);
TextBox1.Text = string.Join(", ", results);
if you want to sum them, just use Enumerable.Sum
如果你想总结它们,只需使用
Enumerable.Sum
TextBox1.Text = $"{results.Sum():N3}";
Update 更新
public static class MyExtension
{
public enum SizeUnits
{
Byte, KB, MB, GB, TB, PB, EB, ZB, YB
}
public static string ToSize(this Int64 value, SizeUnits unit)
{
return (value / (double)Math.Pow(1024, (Int64)unit)).ToString("0.00");
}
}
TextBox1.Text = results.Sum().ToSize();
If you don't want to add complexity by using LINQ and want to practice with arrays: 如果您不想通过使用LINQ来增加复杂性并想要使用数组练习:
var Files = Directory.GetFiles(Path, "*" + filetype, SearchOption.AllDirectories);
long length = 0;
for (int i = 0; i < Files.Length; i++)
{
length += new FileInfo(Files[i]).Length;
}
以下代码可能对您有用。
var FilesAndSizes = Directory.GetFiles(Path, "*" + filetype, SearchOption.AllDirectories).Select(item => new KeyValuePair<string,int>(item, new System.IO.FileInfo(item).Length));
Using Directory.EnumerateFiles
you're able to calculate the total size while only traversing the array once. 使用
Directory.EnumerateFiles
您只需遍历数组一次即可计算总大小。
To get the total size for all files of an extension: 要获取扩展的所有文件的总大小:
long totalSizeInBytes = 0;
foreach(var file in Directory.EnumerateFiles(Path, "*" + filetype, SearchOption.AllDirectories))
{
totalSizeInBytes += new FileInfo(file).Length;
}
To get a list of all file sizes: 要获取所有文件大小的列表:
var results = Directory.EnumerateFiles(Path, "*" + filetype, SearchOption.AllDirectories)
.Select(file => new FileInfo(file).Length);
TextBox1.Text = string.Join(", ", results);
i think that DirectoryInfo object cuold be smarter for your case istead of Directory object. 我认为DirectoryInfo对象对你的情况更聪明,而不是Directory对象。
Look at that example: 看看那个例子:
public static void Main()
{
string filetype = ".jpg";
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("c:\\");
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles("*" + filetype, SearchOption.AllDirectories);
// Display the names and sizes of the files.
Console.WriteLine("The directory {0} contains the following files:", di.Name);
foreach (FileInfo f in fiArr)
Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length);
}
the GetFiles method returns an array of FileInfo objects, where you can find the filesize. GetFiles方法返回FileInfo对象的数组,您可以在其中找到filesize。
Parameters: 参数:
I this example i'm writing to console output but in the same way you can add the text to your textbox. 我这个例子我写的是控制台输出,但同样地,你可以将文本添加到文本框中。
mytexbox.Text += String.Format("The size of {0} is {1} bytes.\r\n", f.Name, f.Length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.