C# FileInfo CopyTo(String)

Description

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

Syntax

FileInfo.CopyTo(String) has the following syntax.


public FileInfo CopyTo(
  string destFileName
)

Parameters

FileInfo.CopyTo(String) has the following parameters.

  • destFileName - The name of the new file to copy to.

Returns

FileInfo.CopyTo(String) method returns A new file with a fully qualified path.

Example

The following example demonstrates the CopyTo method.


//w ww .  ja  v a  2s  .  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);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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