C# FileInfo CopyTo(String, Boolean)

Description

FileInfo CopyTo(String, Boolean) Copies an existing file to a new file, allowing the overwriting of an existing file.

Syntax

FileInfo.CopyTo(String, Boolean) has the following syntax.


public FileInfo CopyTo(
  string destFileName,
  bool overwrite
)

Parameters

FileInfo.CopyTo(String, Boolean) has the following parameters.

  • destFileName - The name of the new file to copy to.
  • overwrite - true to allow an existing file to be overwritten; otherwise, false.

Returns

FileInfo.CopyTo(String, Boolean) method returns A new file, or an overwrite of an existing file if overwrite is true. If the file exists and overwrite is false, an IOException is thrown.

Example

The following example demonstrates the CopyTo method.


//  w w w.j  a va 2 s  .c o m
using System;
using System.IO;

class Test
{

    public static void Main()
    {
        string path = @"c:\SoureFile.txt";
        string path2 = @"c:\NewFile.txt";
        FileInfo fi1 = new FileInfo(path);
        FileInfo fi2 = new FileInfo(path2);

        fi1.CopyTo(path2,true);
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter