如何计算所有选定文件的整体大小?

时间:2014-12-04 17:51:27

标签: c# .net winforms

我有这段代码,我使用openFileDialog选择文件:

private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = true;
            openFileDialog1.FileName = "";
            if (this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
                this.txtUploadFile.Text = this.openFileDialog1.FileName;
            FtpProgress.files = this.openFileDialog1.FileNames;
            if (filesn != null)
            {
                label9.Text = (FtpProgress.files.Length + filesn.Length).ToString();
            }
            else
            {
                label9.Text = FtpProgress.files.Length.ToString();
            }
        }

在label9中,我显示现在选择了多少个文件,我想在label10中显示所有选定文件的整体大小。

我该怎么做?

1 个答案:

答案 0 :(得分:6)

您可以使用FileInfo类获取文件的大小,并使用Linq获取总大小:

var totalSize = FtpProgress.files.Sum(f => new FileInfo(f).Length);

这将返回bytes中的总大小。

相关问题