如何从打开文件对话框中获取文件大小?

3
如何获取我在Openfiledialog中选择的文件的文件大小?
4个回答

21
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
    var size = new FileInfo(ofd.FileName).Length;
}

4

由于信息不够详细,我只能告诉您:

new FileInfo(dialog.Filename).Length

2
if (this.myOpenFileDialog.ShowDialog() == DialogResult.OK)
{
    var length = new System.IO.FileInfo(this.myOpenFileDialog.FileName).Length;
}

1
在多选和单选模式下都适用。
 if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            if (openFileDialog1.Multiselect)
            {
                long totalsize = 0;
                foreach (string siz in openFileDialog1.FileNames)
                    totalsize += new FileInfo(siz).Length;
                MessageBox.Show(totalsize.ToString());
            }
            else
            {
                MessageBox.Show(new FileInfo(openFileDialog1.FileName).Length.ToString());
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接