简体   繁体   中英

Getting filesize from OpenFileDialog?

如何在Openfiledialog中获取当前所选文件的文件大小?

You can't directly get it from the OpenFieldDialog.

You need to take the file path and consturct a new FileInfo object from it like this:

var fileInfo = new FileInfo(path);

And from the FileInto you can get the size of the file like this

fileInfo.Length

For more info look at this msdn page.

我认为有3种方法,创建自定义打开对话框或通过代码将视图设置为详细信息或要求用户使用详细信息视图

Without interop and like the first comment, once the dialogue has been complete ie file/s have been selected this would give the size.

public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.Multiselect)
                {
                    long total = 0;
                    foreach (string s in openFileDialog1.FileNames)
                        total += new FileInfo(s).Length;
                    MessageBox.Show(total.ToString());


                }
                else
                {
                    MessageBox.Show(new FileInfo(openFileDialog1.FileName).Length.ToString());
                }


            }
        }

File size during dialogue I feel would need to use interop

Andrew

If you mean when the dialog is running, I suspect you just change the file view to details. However if you mean programmatically I suspect that you'd have to hook a windows message when the file is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM