02-17-2011, 01:44 AM
How to write to a text file
We can StreamWriter class to write to a file. The Write method is used to write to a text file.
In the StreamWriter constructor, the first parameter is the file name and second parameter is optional, boolean type. If second parameter is true, an existing file will be appended with new text. Otherwise it will erase old text and start with new text.
Code:
Sponsored by
DevExpress Free UI Controls
Become a Sponsor
Similar ArticlesMost ReadTop RatedLatest
External Source Directive in VB.NET
WPF Printing in VB.NET
PathGeometry in Silverlight
Geometry Mini-Language in WPF
Rectangle and RectangleGeometries in WPF
More...
Working with Strings in VB.NET
Displaying data in a DataGrid in VB.NET
Creating an Excel Spreadsheet programmatically using VB.NET
Exception Handling in VB.NET
Creating a SQL Server database programmatically using VB.NET
More...
A Simple Web Service in VB.NET
Flash Player Custom Control for ASP.NET 2.0
Saving and reading Objects
Tic Tac Toe Game in VB.NET
Drawing transparent Images and Shapes using Alpha Blending
More...
Exception Handling in VB.NET
WrapPanel control in WPF using VB.NET
TextBox control in WPF using VB.NET
How to Set Focus on a Control in ASP.NET using VB.NET
How to Upload Files in ASP.NET using VB.NET
More...
We can StreamWriter class to write to a file. The Write method is used to write to a text file.
In the StreamWriter constructor, the first parameter is the file name and second parameter is optional, boolean type. If second parameter is true, an existing file will be appended with new text. Otherwise it will erase old text and start with new text.
Module Module1
Sub Main()
Dim fileName As String = "C:\Mahesh\McTextFile.txt"
Using reader As New System.IO.StreamReader(fileName)
While Not reader.ReadLine() Is Nothing
Console.WriteLine(reader.ReadLine())
End While
End Using
Try
Using writer As New System.IO.StreamWriter(fileName, True)
writer.WriteLine("New text")
writer.WriteLine("New text line goes here")
End Using
Catch ex As Exception
End Try
Console.ReadKey()
End Sub
End Module
How to read a text file
We can use StreamReader to read text files. The following code snippet reads a file and writes on the console line by line.
Code:
Module Module1
Sub Main()
Dim fileName As String = "C:\Mahesh\McTextFile.txt"
Using reader As New System.IO.StreamReader(fileName)
While Not reader.ReadLine() Is Nothing
Console.WriteLine(reader.ReadLine())
End While
End Using
Try
Using writer As New System.IO.StreamWriter(fileName, True)
writer.WriteLine("New text")
writer.WriteLine("New text line goes here")
End Using
Catch ex As Exception
End Try
Console.ReadKey()
End Sub
End Module
I Hope That Will Help