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:\test.txt"
NewCopy = "C:\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 "C:\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.
0 comments:
Post a Comment