简体   繁体   English

从OpenFileDialog获取文件大小?

[英]Getting filesize from OpenFileDialog?

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

You can't directly get it from the OpenFieldDialog. 您无法直接从OpenFieldDialog获取它。

You need to take the file path and consturct a new FileInfo object from it like this: 您需要获取文件路径并从中构建一个新的FileInfo对象,如下所示:

var fileInfo = new FileInfo(path);

And from the FileInto you can get the size of the file like this 从FileInto你可以得到像这样的文件的大小

fileInfo.Length

For more info look at this msdn page. 有关更多信息,请查看此msdn页面。

我认为有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. 但是,如果你的意思是编程,我怀疑你必须在选择文件时挂钩Windows消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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