Visual Basic .NET

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

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
Dim NewCopy As String

FileToCopy = “C:UsersOwnerDocumentstest.txt”
NewCopy = “C:UsersOwnerDocumentsNewTest.txt”

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

System.IO.File.Copy( FileToCopy, NewCopy
)
MessageBox.Show(“File Copied”)

End If

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.

Kaynak : https://www.homeandlearn.co.uk/NET/nets8p6.html ‘sitesinden alıntı

Yorum Yap