Home  |  Delphi .net Home  |  System.IO.FileInfo  |  CopyTo Method
  
CopyTo  
Method  
Copies the current file to a new location, optionally with a new name
FileInfo Class
System.IO NameSpace
CF1.  Procedure CopyTo ( TargetFileName : String ) ;
CF2.  Procedure CopyTo ( TargetFileName : String; OverWrite : Boolean ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The current file is copied to the new TargetFileName.
 
If the file already exists, an exception is thrown unless the optional Overwrite is set to true.
Microsoft MSDN Links
System.IO
System.IO.FileInfo
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  FileInfo : System.IO.FileInfo;
  Files    : Array of String;
  Stream   : FileStream;
  i        : Integer;

begin
  // Create a FileInfo object for a text file
  FileInfo := System.IO.FileInfo.Create('DelphiBasics.txt');

  // Create the DelphiBasics.txt file
  Stream := FileInfo.&Create;

  // Close the file
  Stream.Close;

  // List files in the current folder
  Files := System.IO.Directory.GetFiles(Directory.GetCurrentDirectory);

  for i := 0 to Length(Files)-1 do
    Console.WriteLine(System.IO.Path.GetFileName(Files[i]));

  // Copy the file to a new file
  Console.WriteLine;
  Console.WriteLine('Copy DelphiBasics.txt to CopyOfDelphiBasics.txt :');
  Console.WriteLine;

  FileInfo.CopyTo('CopyOfDelphiBasics.txt');

  // List files in the current folder
  Files := System.IO.Directory.GetFiles(Directory.GetCurrentDirectory);

  for i := 0 to Length(Files)-1 do
    Console.WriteLine(System.IO.Path.GetFileName(Files[i]));

  // Clean up - delete the new file
  System.IO.File.Delete('CopyOfDelphiBasics.txt');

  Console.Readline;
end.
   DelphiBasics.txt
   Project1.exe
   Project1.pdb
   Project1.rsp
  
   Copy DelphiBasics.txt to CopyOfDelphiBasics.txt
  
   CopyOfDelphiBasics.txt
   DelphiBasics.txt
   Project1.exe
   Project1.pdb
   Project1.rsp
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author