Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,087 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,755 people online right now. Registration is fast and FREE... Join Now!




Calculate the File Size on Disk in KB

 
Reply to this topicStart new topic

Calculate the File Size on Disk in KB

sonia.sardana
21 Jun, 2008 - 09:18 AM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 37

hey,Frnds I Have tried the Fill. Code,But the Foll. Code returns the no. of charcters of the File,But i want the File Size on Disk in KB.
Can u Plz help me out.
vb
Imports System
Imports System.IO

Dim filelength As Long = 0

Dim objfile As New FileInfo("D:\Documents and Settings\Administrator\Desktop\URLs[1].txt")

filelength = filelength + file.Length
MsgBox(filelength)

User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 02:01 PM
Post #2

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,262



Thanked: 39 times
Dream Kudos: 1675
My Contributions
If all you want to do is get the size of a file in KB, this should do it:
vb

Public Function GetSizeKB(ByVal filename As String) As Double
Dim info As New IO.FileInfo(filename)
Return info.Length / 1000
End Function

User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 09:13 PM
Post #3

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
Sorry to be anal, but...

vb

Public Function GetSizeKB(ByVal filename As String) As Double
' There are 1024 bytes in a kilobyte
Return New IO.FileInfo(filename).Length / 1024
End Function


EDIT: I put a wink in the comments in the source code but it shown the raw HTML sad.gif

This post has been edited by RodgerB: 21 Jun, 2008 - 09:14 PM
User is offlineProfile CardPM
+Quote Post

sonia.sardana
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 10:57 PM
Post #4

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 37

Hey Frnds First of all THXXXX, The above function calculates the Size of one file & everytime we have to give the extension of file such as .txt, .doc.
AS Foll-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filelength As Double
Dim objfile As FileInfo
objfile = New FileInfo("D:\Documents and Settings\Administrator\Desktop\Private Function SaveDetails[1].doc")
filelength = calculate_size(objfile)
MsgBox(filelength)
End Sub

Public Function calculate_size(ByVal file As FileInfo) As Double
calculate_size = file.Length / 1024
End Function

If I want that it calculates the size of any file...Without giving Extension. In that case what i have to do ??/
Suppose i want to cal the size of .Gif file then??????????

This post has been edited by sonia.sardana: 21 Jun, 2008 - 11:01 PM
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 11:48 PM
Post #5

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
I'm not quite following as to what you want. Do you want to code a function that calculates the size of all the files in a folder with a certain extension, or do you want to code something that gets the size of the file without the need to use an extension.

For the latter, why would you want to do it this way?
User is offlineProfile CardPM
+Quote Post

sonia.sardana
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 11:51 PM
Post #6

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 37

No NEED TO REPLY ANYTHING, I GOT IT......I m saying which is not possible..Soryy
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Calculate The File Size On Disk In KB
21 Jun, 2008 - 11:53 PM
Post #7

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
Oh, no problem. Sorry for the misunderstanding. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month