|
Free
computer Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
||||||
How to Copy a File in VB .NET |
|||||||
|
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: 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 FileToCopy = "C:\test.txt" If System.IO.File.Exists(FileToCopy)
= True Then 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.
In the next part, we'll see how to Move a file.
Learn how to Move a File in VB .NET --> <--Back to the .NET Contents Page View all our Home Study Computer Courses
|