Wednesday, October 3, 2012

VB.NET Easy way to write to Text File

Following lines of code is enough to write a string to a text file.

First, import the name space, system.IO

Second, use following code snippet at anywhere in your application which has the requirement to write to a file.

Using sr As StreamWriter = New StreamWriter("myfile.txt", True)
            sr.WriteLine("Hello")

End Using

Explaining the Highlights:
1. "myfile.txt" is the file name you want write to, can include file locations. if the file does not exists in the location, file will be created.
2. True  ,means append to the file if file exist, use false to write new.
3.   sr.WriteLine("Hello") , used to write to the file. 

No comments: