Get Length of the File using LOF Function in VB.NET

In this tutorial, we will create a program that can get the exact length of any kinds of file using the LOF function and without importing the system.IO. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only two Buttons named Button1 and labeled it as "Open a File" and Button2 labeled as "Get Length of the File". Insert an OpenFileDialog named OpenFileDialog1 for opening any types of file. Add also a textbox named TextBox1 for displaying the filename after browsing with the OpenFileDialog. You must design your interface like this: design 3. For button1_click as Open File button, put this code below.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. OpenFileDialog1.ShowDialog()
  3. TextBox1.Text = OpenFileDialog1.FileName
  4. End Sub
We have used an OpenFileDialog in showing and browsing a desired file to open. This will locate all types of files in your computer. Then after that, the desired file you have chose (the filename) is displayed in TextBox1. 4. For button2_click as Get Length of File button, put this code below. Initialized variable length as long integer
  1. Dim length As Long
Open a file in your desired file path and file name inputted in TextBox1  FileOpen(1, TextBox1.Text, OpenMode.Input) Get length of file. This will return the size of the file in Bytes using the LOF Function length = LOF(1) Display the Length of the File in MessageBox  MsgBox("The length of the file is: " & length & " bytes") Close the file  FileClose(1)  

Output:

output Download the source code below and try it! :) For more inquiries just contact my number or e-mail below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer Mobile: 09102918563 Telephone: 826-9296 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Add new comment