Retrieving files from OpenFileDialog?

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

+6
c # openfiledialog
source share
4 answers

You cannot directly get it from OpenFieldDialog.

You need to take the path to the file and create a new FileInfo object from it as follows:

var fileInfo = new FileInfo(path); 

And from FileInto you can get a file size similar to this

 fileInfo.Length 

See the msdn page for more information.

+6
source share

I think that there are 3 ways, creating your user open dialog or setting the view as a part by code or inviting the user to use the detailed view

+1
source share

Without interop and as the first comment, as soon as the dialogue is completed, i.e. / s file was selected, this will give a 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 the dialog I think you will need to use interop

Andrew

+1
source share

If you mean when the dialog is running, I suspect that you just changed the file view to details. However, if you mean programmatically, I suspect that you will need to include a Windows message when selecting the file.

0
source share

All Articles