Read Text File using Classic VB
In classic VB reading a file is not that easy as we do in vb.net. The first and easy way to access is using the simple file open method available in classic Visual Basic. Using this method, we can allocate a unique file number for opening the file. Further the same number has to be used for closing the file handle.
In addition to this, there are various modes a file can be opened. Some of them are Append, Binary, Input, Output, or Random. By default Random will be used.
Read text file in VB.Net
In VB.net we can read files very easily. The System.IO namespace contains really very useful set of functions which can reduce the effort and time in coding file related activities. One of the easiest is reading into string System.IO.File.ReadAllText(FilePath).
Using FileSystemObject in VB6 to Read a Text File
With FileSystemObject File IO operations are very easier in VB. Almost all the required functions are well defined. For reading, we have to use OpenTextFile method in the FileSystemObject. Once opened, A TextStream object can be used to fetch the data. To fetch line by line we need to loop through until we reach EOF. To find the EOF, FileSystemObject has a property AtEndOfStream. When it is true, it represents that the end of file is reached.
Using the Open method in VB6
Using a open method is much easier and effective way. It is well supported from a earlier versions of the visual basic. Because of it’s simplicity, we can see this in a lot of applications. Once it is opened please close it immediately after done working with the whole text fetched. This open method is working based on FileNumber. So the same FileNumber has to be used to close as well.
Search using Regular Expressions
If search string is not specified the function will give the entire content. If the search string is specified then the search results will be appended to a text and will be returned. The search is regular expression based search
Since the search is regular expression based, the regular expression should be validated before using the function. Or else the function will be hanging due to invalid regular expression.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = System.IO.File.ReadAllText(TextBox1.Text)