Home and Learn: VB Net Course
This lesson is part of an ongoing tutorial. The first part is here: How to open a Text 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:
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
The file we want to copy is called "test.txt". We've put this inside of a string variable called FileToCopy. 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.
Buy the Book of this Course
Email us: enquiry at homeandlearn.co.uk