Description |
The Create method should really have been named CreateFile so as to distinguish from the Create constructor (as is the case in DirectoryInfo.CreateDirectory).
It creates the file whose name was specified in the FileInfo constructor, returning a FileStream object allowing access to the file.
|
| Notes | Important : you must prefix Create with an & to distinguish from the Create keyword.
Important : you should assign the result of the Create to a variable and close that stream before using other methods such as CopyTo. Otherwise you will get an IO Exception to say that the file is in use!
|
|
Microsoft MSDN Links |
System.IO
System.IO.FileInfo
|
|
|
A simple example |
program Project1;
{$APPTYPE CONSOLE}
uses
System.IO;
var
FileInfo : System.IO.FileInfo;
Stream : FileStream;
Files : Array of String;
i : Integer;
begin
// 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]));
// Create a FileInfo object for a text file
Console.WriteLine;
Console.WriteLine('Creating DelphiBasics.txt :');
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 again
Console.WriteLine;
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
FileInfo.Delete;
Console.Readline;
end.
| Show full unit code | Project1.exe
Project1.pdb
Project1.rsp
Creating DelphiBasics.txt :
DelphiBasics.txt
Project1.exe
Project1.pdb
Project1.rsp
|
|
|
|