Bewerken

Delen via


FileSystem.CopyFile Method

Definition

Copies a file to a new location.

Overloads

CopyFile(String, String)

Copies a file to a new location.

CopyFile(String, String, UIOption)

Copies a file to a new location.

CopyFile(String, String, Boolean)

Copies a file to a new location.

CopyFile(String, String, UIOption, UICancelOption)

Copies a file to a new location.

CopyFile(String, String)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

public:
 static void CopyFile(System::String ^ sourceFileName, System::String ^ destinationFileName);
public static void CopyFile (string sourceFileName, string destinationFileName);
static member CopyFile : string * string -> unit
Public Shared Sub CopyFile (sourceFileName As String, destinationFileName As String)

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

A file in the target directory with the same name is in use.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

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\test.txt", overwrite:=False)

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.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

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

Remarks

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.

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

See also

Applies to

CopyFile(String, String, UIOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

public:
 static void CopyFile(System::String ^ sourceFileName, System::String ^ destinationFileName, Microsoft::VisualBasic::FileIO::UIOption showUI);
public static void CopyFile (string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI);
static member CopyFile : string * string * Microsoft.VisualBasic.FileIO.UIOption -> unit
Public Shared Sub CopyFile (sourceFileName As String, destinationFileName As String, showUI As UIOption)

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

showUI
UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

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\test.txt", overwrite:=False)

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.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

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

Remarks

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.

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

See also

Applies to

CopyFile(String, String, Boolean)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

public:
 static void CopyFile(System::String ^ sourceFileName, System::String ^ destinationFileName, bool overwrite);
public static void CopyFile (string sourceFileName, string destinationFileName, bool overwrite);
static member CopyFile : string * string * bool -> unit
Public Shared Sub CopyFile (sourceFileName As String, destinationFileName As String, overwrite As Boolean)

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

overwrite
Boolean

True if existing files should be overwritten; otherwise False. Default is False.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

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\test.txt", overwrite:=False)

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.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

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

Remarks

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.

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

See also

Applies to

CopyFile(String, String, UIOption, UICancelOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

public:
 static void CopyFile(System::String ^ sourceFileName, System::String ^ destinationFileName, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::UICancelOption onUserCancel);
public static void CopyFile (string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel);
static member CopyFile : string * string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.UICancelOption -> unit
Public Shared Sub CopyFile (sourceFileName As String, destinationFileName As String, showUI As UIOption, onUserCancel As UICancelOption)

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

showUI
UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

onUserCancel
UICancelOption

Specifies what should be done if the user clicks Cancel during the operation. Default is ThrowException.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

UICancelOption is set to ThrowException, and the user has canceled the operation or an unspecified I/O error occurs.

Examples

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\test.txt", overwrite:=False)

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.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

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

Remarks

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.

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

See also

Applies to