1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
FileInfo class namespace
System.IO
Declaration
type FileInfo = class sealed(FileSystemInfo)
Description
Provides instance 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 FileInfo class:
program System_IO_FileInfo;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}

uses System.IO;

type
  TestFileInfo = class
  public
    class procedure Main;
  end;

{ TestFileInfo }

class procedure TestFileInfo.Main;
var
  fi, fi2: FileInfo;
  s : string;
  path: string;
  path2: string;
begin
  path := 'c:\temp\MyTest.txt';
  fi := FileInfo.Create(path);

  if (fi.Exists = False) then
  begin
    // Create a file to write to.
    with fi.CreateText do
    try
      WriteLine('Hello');
      WriteLine('And');
      WriteLine('Welcome');
    finally
      //dispose StreamWriter
      Free;
    end;

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

  try
    path2 := (path + 'temp');
    fi2 := FileInfo.Create(path2);

    //Ensure that the target does not exist
    fi2.Delete;

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

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

//test program
begin
  TestFileInfo.Main;
end.
Example output
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

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.