Export (0) Print
Expand All
Expand Minimize

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.

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).

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

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

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.

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

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.

Community Additions

ADD
Show:
© 2014 Microsoft