Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi .I am working on a program in visual basic .I want that when i select a file in open file dialog its size (mb) to show in label.I have worked a lot but it always shows error
I need a quick help

What I have tried:

VB
If dlg.showdialog =dialog result .ok then
  Panel1.visible = true
  Label1.visible = false
  lbl_size.visble=true
  lbl_size.text=  dlg.filesize
End if
Posted
Updated 17-Jun-20 3:17am
v2

If you're using OpenFileDialog Class (System.Windows.Forms)[^], then this class does NOT have FileSize property.


Use FileInfo(String) Constructor (System.IO)[^] to get file properties, details.
VB
Dim fi As FileInfo = New FileInfo(flg.FileName)
lbl_size.text = fi.Length
 
Share this answer
 
v2
Comments
CPallini 17-Jun-20 2:21am    
5.
Maciej Los 17-Jun-20 2:27am    
Thank you, Carlo.
Member 14839888 17-Jun-20 9:13am    
Thanks .I got it . it works good
[no name] 22-Jun-20 13:21pm    
+5
Maciej Los 22-Jun-20 13:39pm    
Thank you, Bruno.
I worked a more and found a good solution myself
Here it is:
Dim filename As String = dlg.FileName
        Dim fi As New IO.FileInfo(filename)
        Dim exists As Boolean = fi.Exists
        If fi.Exists Then
            Console.WriteLine("File Exists")
        End If
        Dim size As Long = fi.Length
        Dim dormat As String = fi.Extension
        Console.WriteLine("File Size in Bytes:{0}", size)
        Console.ReadLine()
        Label9.Text = (size / 1024) & " KB"
 
Share this answer
 
Comments
Maciej Los 17-Jun-20 9:30am    
Yeah, you did it "yourself" without someone's help.
Richard MacCutchan 17-Jun-20 10:34am    
Yes, and there is some complex mathematics on the last line.
Maciej Los 18-Jun-20 1:59am    
:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900