Share via


My.Computer.FileSystem.CopyFile Method 

Copies a file to a new location.

' Usage
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,overwrite)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI ,onUserCancel)
' Declaration
Public Sub CopyFile( _
   ByVal sourceFileName As String, _
   ByVal destinationFileName As String _
)
' -or-
Public Sub CopyFile( _
   ByVal sourceFileName As String, _
   ByVal destinationFileName As String, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub CopyFile( _
   ByVal sourceFileName As String, _
   ByVal destinationFileName As String, _
   ByVal showUI As UIOption _
)
' -or-
Public Sub CopyFile( _
   ByVal sourceFileName As String, _
   ByVal destinationFileName As String, _
   ByVal showUI As UIOption, _
   ByVal onUserCancel As UICancelOption _
)

Parameters

  • sourceFileName
    String. The file to be copied. Required.
  • destinationFileName
    String. The location to which the file should be copied. Required.
  • overwrite
    Boolean. Whether existing files should be overwritten. Default is False. Required.
  • showUI
    UIOption. Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.
  • onUserCancel
    UICancelOption. Specifies what should be done if the user clicks Cancel during the operation. Default is ThrowException. Required.

Exceptions

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 \\.\) (ArgumentException).

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

  • destinationFileName contains path information (ArgumentException).

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

  • destinationFileName is Nothing or an empty string (ArgumentNullException).

  • The source file is not valid or does not exist (FileNotFoundException).

  • The combined path points to an existing directory (IOException).

  • The destination file exists and overwrite is set to False (IOException).

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

  • A file in the target directory with the same name is in use (IOException).

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

  • UICancelOption is set to ThrowException, and the user has canceled the operation (OperationCanceledException).

  • UICancelOption is set to ThrowException, and an unspecified I/O error occurs (OperationCanceledException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

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

  • The user lacks necessary permissions to view the path (SecurityException).

Remarks

CopyFile does not preserve ACEs (Access Control Entries). The newly created file inherits default ACEs from the directory in which it is created.

Tasks

The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.

To See

Copy a file to the same directory.

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

Copy a file to a different directory.

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

Example

This example copies the fileTest.txtto the directoryTestFiles2without overwriting existing files.

My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2")

Replace the file paths with the paths you want to use in your code.

This example copies the fileTest.txtto the directory TestFiles2 and renames it NewFile.txt.

My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2","NewFile.txt", FileIO.UICancelOption.DoNothing)

Replace the file paths with the paths you want to use in your code.

Requirements

Namespace: Microsoft.VisualBasic.MyServices

Class: FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permissions may be necessary:

Permission Description

EnvironmentPermission

Controls the ability to access all environment variables. Associated enumeration: Unrestricted.

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

RegistryPermission

Controls the ability to access registry variables. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Copy Files with a Specific Pattern to a Directory in Visual Basic
How to: Create a Copy of a File in the Same Directory in Visual Basic
How to: Copy a Directory to Another Directory in Visual Basic
How to: Rename a File in Visual Basic

Reference

My.Computer.FileSystem Object
UICancelOption Enumeration