6

How can I get the filesize of the currently-selected file in my Openfiledialog?

Sampson
  • 265,109
  • 74
  • 539
  • 565
Mary
  • 581
  • 4
  • 8
  • 16

4 Answers4

6

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.

Alex Shnayder
  • 1,362
  • 1
  • 13
  • 24
2

Without interop and like the first comment, once the dialogue has been complete i.e. 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

REA_ANDREW
  • 10,666
  • 8
  • 48
  • 71
1

I think there is 3 way, creating your custom open dialog or setting by code the view as detail or asking the user to use detail view

Fredou
  • 19,848
  • 10
  • 58
  • 113
0

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.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216