Saturday, 15 September 2012

Appending Text to a File in VB .NET

There will be times when you won't want to erase all the text from your file. You'll only want to add text to what you currently have. In which case you need to Append.

Appending text to your file is quite easy.

When you set up the object variable for the StreamWriter, you just typed the name and path of the file:

Dim objWriter As New System.IO.StreamWriter( FILE_NAME )

To append text to a file, you type a comma after your file name then type the word True:

Dim objWriter As New System.IO.StreamWriter( FILE_NAME, True )

If you want to add some text to the file, you need that True value. If you leave out the True or False, a new file is not created.

Here some code we wrote to that appends text to the file:

Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
Dim i As Integer
Dim aryText(4) As String

aryText(0) = "Mary WriteLine"
aryText(1) = "Had"
aryText(2) = "Another"
aryText(3) = "Little"
aryText(4) = "One"

Dim objWriter As New System.IO.StreamWriter( FILE_NAME, True )

For i = 0 To 4

objWriter.WriteLine(aryText(i))

Next

objWriter.Close()

MsgBox("Text Appended to the File")

The lines that have changed are in bold. But, as you can see, not much has changed! But try both version and see how they work.

 

Creating a file if it doesn't exist

If you want to create a file if one doesn't exist, the process is again quite simple:

Dim objWriter As New System.IO.StreamWriter( FILE_NAME, False )

This time, we've just added the word "False" to the end of FILE_NAME. This will ensure that a new text file is created if one doesn't exist.

In the next part, we'll see how to copy a file in VB .NET.

How to Copy a File in VB .NET

You can also copy a file that you've created. This time, we don't need the StreamWriter or StreamReader of System.IO. We need the File object:

System.IO.File

This just means "System.IO has an object called File. Use this File object".

File has it's own properties and methods you can use. One of these is Copy. Here's some code that makes a copy of our test file .

Dim FileToCopy As String
Dim NewCopy As String

FileToCopy = "C:\Users\Owner\Documents\test.txt"
NewCopy = "C:\Users\Owner\Documents\NewTest.txt"

If System.IO.File.Exists( FileToCopy ) = True Then

System.IO.File.Copy( FileToCopy, NewCopy )
MsgBox("File Copied")

End If

The file we want to copy is called "test.txt". We've put this inside of a string variable calledFileToCopy. The name of the new file we want to create, and its location, are assigned to a variable called NewCopy.

Next, we have to check to see if the file we're trying to copy exists. Only if it does should we go ahead and copy it. You've met this code before. Inside of the If Statement, we have this:

System.IO.File.Copy( FileToCopy, NewCopy )

We use the Copy method of System.IO.File. In between the round brackets, you first type the name of the file you want to copy. After a comma, you then type the name of the new file and its new location.

In the next part, we'll see how to Move a file.