|
|
Title | Use routines to easily read and write a file's contents in VB .NET |
Description | This example shows how to use routines to easily read and write a file's contents in Visual Basic .NET. |
Keywords | star, polygon, VB.NET |
Categories | Files and Directories, VB.NET |
|
|
Function GetFileContents creates a StreamReader attached to the file and uses its ReadToEnd method to read the file. It closes the StreamReader and returns the file's contents.
|
|
' Return a file's contents.
Private Function GetFileContents(ByVal file_name As String) _
As String
Dim stream_reader As New IO.StreamReader(file_name)
Dim txt As String = stream_reader.ReadToEnd()
stream_reader.Close()
Return txt
End Function
|
|
Subroutine SetFileContents creates a StreamWriter attached to the file, uses its Write method to write the file, and then calls its Close method.
|
|
' Set a file's contents.
Private Sub SetFileContents(ByVal file_name As String, _
ByVal contents As String)
Dim stream_writer As New IO.StreamWriter(file_name, _
False)
stream_writer.Write(contents)
stream_writer.Close()
End Sub
|
|
|
|
|
|