• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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


how do i get the size of a file?

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


0
kamleshmistry
Asked:
kamleshmistry
3 Solutions
 
gregoryyoungCommented:
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
 
kamleshmistryAuthor Commented:


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
 
GrumbleBotCommented:
       Dim fi As FileInfo = New FileInfo("D:\test.txt")
        MessageBox.Show(fi.Length.ToString())

hope this helps.

Good luck
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gregoryyoungCommented:
that is correct.
0
 
Timbo87Commented:
Dim fs As New FileStream("c:\file.jpg", FileMode.Open)
Console.WriteLine(fs.Length)
0
 
gregoryyoungCommented:
timbo why open the file when you dont have to ?
0
 
Timbo87Commented:
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
 
plqCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now