Skip to content

Latest commit

 

History

History
62 lines (40 loc) · 3.45 KB

how-to-create-a-copy-of-a-file-in-a-different-directory.md

File metadata and controls

62 lines (40 loc) · 3.45 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: How to: Create a Copy of a File in a Different Directory in Visual Basic
How to: Create a Copy of a File in a Different Directory
07/20/2015
My.Computer.FileSystem.CopyFile method, copying files [Visual Basic]
files [Visual Basic], copying
CopyFile method [Visual Basic], copying files in Visual Basic
I/O [Visual Basic], copying files
88e2145c-d414-45a5-ad03-6f5d58ecca26

How to: Create a Copy of a File in a Different Directory in Visual Basic

The My.Computer.FileSystem.CopyFile method allows you to copy files. Its parameters provide the ability to overwrite existing files, rename the file, show the progress of the operation, and allow the user to cancel the operation.

To copy a text file to another folder

  • Use the CopyFile method to copy a file, specifying a source file and the target directory. The overwrite parameter allows you to specify whether or not to overwrite existing files. The following code examples demonstrate how to use CopyFile.

    [!code-vbVbFileIOMisc#24]

Robust Programming

The following conditions may cause an exception to be thrown:

  • The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (xref:System.ArgumentException).

  • The system could not retrieve the absolute path (xref:System.ArgumentException).

  • The path is not valid because it is Nothing (xref:System.ArgumentNullException).

  • The source file is not valid or does not exist (xref:System.IO.FileNotFoundException).

  • The combined path points to an existing directory (xref:System.IO.IOException).

  • The destination file exists and overwrite is set to False (xref:System.IO.IOException).

  • The user does not have sufficient permissions to access the file (xref:System.IO.IOException).

  • A file in the target folder with the same name is in use (xref:System.IO.IOException).

  • A file or folder name in the path contains a colon (:) or is in an invalid format (xref:System.NotSupportedException).

  • ShowUI is set to True, onUserCancel is set to ThrowException, and the user has cancelled the operation (xref:System.OperationCanceledException).

  • ShowUI is set to True, onUserCancel is set to ThrowException, and an unspecified I/O error occurs (xref:System.OperationCanceledException).

  • The path exceeds the system-defined maximum length (xref:System.IO.PathTooLongException).

  • The user does not have required permission (xref:System.UnauthorizedAccessException).

  • The user lacks necessary permissions to view the path (xref:System.Security.SecurityException).

See also