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:

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 file Test.txt to the directory TestFiles2 without 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 file Test.txt to 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