1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
File class namespace
System.IO
Declaration
type File = class sealed;
Description
Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
Example
The following example demonstrates some of the main members of the File class.
program System_IO_File;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}


uses
  System.IO;
  
type
  FileTest = class
  public
    class procedure Main;
  end;

{ FileTest }

class procedure FileTest.Main;
var
  path2: string;
  path: string;
  s : string;
begin
  path := 'c:\temp\MyTest.txt';
  if (&File.Exists(path) = False) then
  begin
    // Create a file to write to.
    with &File.CreateText(path) do
    try
      WriteLine('Hello');
      WriteLine('And');
      WriteLine('Welcome');
    finally
      //dispose StreamWriter
      Free;
    end;

    // Open the file to read from.
    with &File.OpenText(path) do
    try
      s := '';
      s := ReadLine;
      while (s <> '') do
      begin
        Console.WriteLine(s);
        s := ReadLine;
      end;
    finally
      //dispose StreamReader
      Free;
    end;
  end;

  try
    path2 := (path + 'temp');

    // Ensure that the target does not exist.
    &File.Delete(path2);

    // Copy the file.
    &File.Copy(path, path2);
    Console.WriteLine('{0} was copied to {1}.', 
                      path, 
                      path2);

    // Delete the newly created file.
    &File.Delete(path2);
    Console.WriteLine('{0} was successfully deleted.', 
                      path2);
  except
    on e: Exception do
      Console.WriteLine('The process failed: {0}', 
                        e.ToString);
  end;
end; //FileTest.Main

begin //FileTest.Main
  FileTest.Main;
end.
Example output
Hello
And
Welcome
c:\temp\MyTest.txt was copied to c:\temp\MyTest.txttemp.
c:\temp\MyTest.txttemp was successfully deleted.

Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.