Solved

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

Posted on 2004-08-27
8
146 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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11918288
that is correct.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Custom auto number 6 51
MS access and calling saved Queries 4 26
Showdialog 8 20
Vb.Net Date Formatting Assistance 4 0
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

20 Experts available now in Live!

Get 1:1 Help Now