Solved

how to i get the size in bytes of a file in vb.net

Posted on 2004-08-27
8
149 Views
Last Modified: 2010-08-05

how do i get the size of a file?

system.file.io.files.getattributes("path").size? ... no, that's not right....


0
Comment
Question by:kamleshmistry
8 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11917723
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofileinfoclasslengthtopic.asp

from MSDN:

' The following example displays the names and sizes
' of the files in the specified directory.
Imports System
Imports System.IO

Public Class FileLength

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("c:\")
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names and sizes of the files.
        Dim f As FileInfo
        Console.WriteLine("The directory {0} contains the following files:", di.Name)
        For Each f In fiArr
            Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length)
        Next f
    End Sub 'Main
End Class 'FileLength
0
 

Author Comment

by:kamleshmistry
ID: 11918226


not bad, but how do I get the size of a particular file, provided I know the path

Input:  file path

Output:  file size...
0
 
LVL 3

Accepted Solution

by:
GrumbleBot earned 150 total points
ID: 11918283
       Dim fi As FileInfo = New FileInfo("D:\test.txt")
        MessageBox.Show(fi.Length.ToString())

hope this helps.

Good luck
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11918288
that is correct.
0
 
LVL 15

Assisted Solution

by:Timbo87
Timbo87 earned 50 total points
ID: 11918291
Dim fs As New FileStream("c:\file.jpg", FileMode.Open)
Console.WriteLine(fs.Length)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11918301
timbo why open the file when you dont have to ?
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 11918355
If you're using a method that takes in a Stream, it will provide the stream and the file size. Considering he has one specific file, I have a feeling he's going to open it anyways.
0
 
LVL 8

Assisted Solution

by:plq
plq earned 50 total points
ID: 11919520
This is not using .NET framework but it will work fine in .NET and is so simple...

Public Function FileSize(sFileName As String) As Integer

    Dim scr As Object
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    FileSize = scr.GetFile(sFileName).Size
    Set scr = Nothing
   
End Function

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 2005 Tooltips on Form Load Event 15 29
Error handling in asp.net site 5 23
No Data for DropDown List 2 22
vb.net 1 month apart 11 24
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now