how to i get the size in bytes of a file in

Posted on 2004-08-27
Last Modified: 2010-08-05

how do i get the size of a file?"path").size? ... no, that's not right....

Question by:kamleshmistry
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 37

Expert Comment

ID: 11917723

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

Author Comment

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...

Accepted Solution

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

hope this helps.

Good luck
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

LVL 37

Expert Comment

ID: 11918288
that is correct.
LVL 15

Assisted Solution

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

Expert Comment

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

Expert Comment

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.

Assisted Solution

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


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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