|
Read content from a text file and write content to a text file using VB.NET |
|
|
|
|
Source Codes -
.NET
|
|
Written by Thomas Kaloyani
|
Usage:
Dim s as string = ReadFile("C:\test.txt")
debug.writeline(s)
SaveFile "C:\Test.txt", "Successful writing"
Declarations:
Imports System.IO
Code:
Public Function ReadFile(ByVal strPath As String) As String
Dim strContent As String
Dim obj As StreamReader
Try
obj = New StreamReader(strPath)
strContent = obj.ReadToEnd()
obj.Close()
Return strContent
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Return ""
Finally
obj.Close()
obj = Nothing
End Try
End Function
Public Function SaveFile(ByVal strPath As String, ByVal strContent As String) As Boolean
Dim obj As StreamWriter
Try
obj = New StreamWriter(strContent)
obj.Write(strContent)
obj.Close()
Return True
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Return False
Finally
obj.Close()
obj = Nothing
End Try
End Function
About the author: Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website at www.Kaloyani.com
Source: www.VBprofs.com |