Get size of a file

Snookie

Active member
Joined
Dec 13, 2010
Messages
29
Programming Experience
Beginner
Hey all.

I have a problem,
I try to open a file, then i open the file, i want to get the size of the file and write in to a ListBox.

So if i have a file : 123,00 kb so then i push the button Ok then get the size of the file and write the size into the Listbox.

Cant some one here tell me how i doit, i have try and try, but its not work for me.

Here its my code.:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim alleAttr As Long
If System.IO.File.Exists(lblfilnavn.Text) Then
lstAttr.Items.Clear()
lstAttr.Items.Add("Make : " & System.IO.File.GetCreationTime(lblfilnavn.Text))
lstAttr.Items.Add("Open : " & System.IO.File.GetLastAccessTime(lblfilnavn.Text))
lstAttr.Items.Add("Change : " & System.IO.File.GetLastWriteTime(lblfilnavn.Text))

*********** Its that line i have problem with i cannot see how i doit.
lstAttr.Items.Add("Size : " & System.IO.File.Length(lblfilnavn.Text))
And i have try this one to
lstAttr.Items.Add("Size : " & System.IO.File.GetLength(lblfilnavn.Text))

alleAttr = System.IO.File.GetAttributes(lblfilnavn.Text)
lstAttr.Items.Add("")
lstAttr.Items.Add("Hidden : " & CBool(alleAttr And IO.FileAttributes.Hidden))
lstAttr.Items.Add("System : " & CBool(alleAttr And IO.FileAttributes.System))
lstAttr.Items.Add("ReadOnly : " & CBool(alleAttr And IO.FileAttributes.ReadOnly))
lstAttr.Items.Add("Arkivbit : " & CBool(alleAttr And IO.FileAttributes.Archive))
End If
End Sub

I hope some one here could help me out
 
I am not sure what you mean..

Cant I not use the code i have .?

If i not cant use it, can you give me a example of some code i shell use...
 
So you give some broken code and ask for advice on how to fix it. When you're given the advice you say 'Why can't I use my (broken) code?'.

VB.NET:
        Dim fi As New IO.FileInfo("C:\Temp\states.txt")
        Dim attr = fi.Attributes
        With ListBox1.Items
            .Add("Make: " & fi.CreationTime.ToString)
            .Add("Open: " & fi.LastAccessTime.ToString)
            .Add("Change: " & fi.LastWriteTime.ToString)
            .Add("Size: " & fi.Length.ToString & " bytes")
            .Add("Hidden: " & CBool(attr And IO.FileAttributes.Hidden))
            .Add("System: " & CBool(attr And IO.FileAttributes.System))
            .Add("ReadOnly: " & CBool(attr And IO.FileAttributes.ReadOnly))
            .Add("Archive: " & CBool(attr And IO.FileAttributes.Archive))
        End With
 
My code works fine if i take this out
*********** Its that line i have problem with i cannot see how i doit.
lstAttr.Items.Add("Size : " & System.IO.File.Length(lblfilnavn.Text))
And i have try this one to
lstAttr.Items.Add("Size : " & System.IO.File.GetLength(lblfilnavn.Text))

All the other code i have there works fine...
Itry to put the line there could show me the size of the file, and thats whas my problem. and i have hope that whas because i have wrote wrong.
And the other way you say i could doit i dont know the code to doit...

But i am happy to the code toy have wrote for me.
And i will try the code...
 
MattP

Thanks for the code...
Its realy works nice..

But what ar the defrence between the code you wrote and what i use before...
In the code i use, Its not Possible to do the file size ? or what..?
Or its a old shcool to do it...
Its so i can better understand why so i could learn on my mistake in my code.. ;-)

But Thanks again. :)
 
Back
Top