' Copy one file to a new location.
' Display file contents.
End ModuleHow are you today?
How are you today?
Overwrite. For File.Copy to succeed, the destination file must not exist. You can override this behavior by passing True as the third argument.
And This eliminates the "already exists" exception. The previous contents of the destination file are lost.
' Allow the destination to be overwritten.
File.Copy("file-a.txt", "file-b.txt", True)
End ModuleI am well, thank you.
I am well, thank you.
A discussion. A file can be copied by loading it into memory (like with File.ReadAllText), and then writing that data to a new file. But this involves more than an operating system call.
However We may need to do further processing on the file. In this case it may be more efficient to just write the in-memory version to the disk.
Tip Using File.Copy is usually the better choice, but this is not always true. Sometimes it can be avoided.
A summary. With File.Copy the original file must exist. And the destination file must not exist, unless you specify overwriting. It may cause exceptions—using try and catch is worthwhile.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 24, 2022 (edit link).