views:

673

answers:

5

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

+1  A: 

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  A: 

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
A: 

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
A: 

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
A: 

hi thanx for answers i want get filesize after the filedialog has been closed and show it i get file path from [System.IO.Path.GetDirectoryName(op.FileName);] but i can not find FILEINFo class

Mary
System.IO.FileInfo
ThePower
oh thanx ThePower
Mary