How can I get the filesize of the currently-selected file in my Openfiledialog?
-
4Do you mean while the dialogue is open, or after the dialogue has been closed? Do you mean for the program or for the user of the program? – Richard Aug 24 '09 at 11:10
-
I have a feeling that some interop will be needed here :-) Nice question though BTW, Interesting – REA_ANDREW Aug 24 '09 at 11:14
-
More clarification please, as per Richard's comment. – Ian Kemp Aug 24 '09 at 11:19
4 Answers
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.

- 1,362
- 1
- 13
- 24
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

- 10,666
- 8
- 48
- 71
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

- 19,848
- 10
- 58
- 113
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.

- 64,563
- 18
- 145
- 216