SteveOH

ASP.NET / VB.NET: Get File Size

by Steve Hernandez on Jul.11, 2008, under Technology

If you want to get the size of a file using ASP.NET (VB), here’s how.  I used this to check if the file is big enough.  If it’s too small (in my application), an error existed and the user was told to contact the Administrator.

Dim fileName As String = "myFile.jpg"
Dim myFileInfo As New FileInfo(Server.MapPath(fileName))
Dim fileSizeBytes As UInt32 = myFileInfo.Length

Response.Write("File: " & fileName & "<br>Bytes (B): " & fileSizeBytes & _
               "<br>Kilo Bytes (KB): " & fileSizeBytes / 1024 & "<br>Mega Bytes (MB): " & _
               fileSizeBytes / (1024 ^ 2) & "<br>Giga Bytes (GB): " & _
               fileSizeBytes / (1024 ^ 3))

Note that this only works for files that already exist ‘locally’, not those that are in ‘transport’.  Meaning, you can use this for files sitting on the local server, over the network, etc, but you cannot for files pre- or during uploading, only after they’ve been uploaded and saved to the server.

 I hope this saves you some time… or at least a lengthy search on Google.

:, ,

2 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...