Home  |  Delphi .net Home  |  System.IO.File  |  Create Method
Create  
Method  
Creates a file with the specified file name
File Class
System.IO NameSpace
CF1.  Function Create ( PathString : String; ) : FileStream;
CF2.  Function Create ( PathString:StringPathString : String; BufferSize : Integer; ) : FileStream; Static;
CF : Methods with this mark are Compact Framework Compatible
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 PathString value, returning a FileStream object allowing access to the file.
 
Optionally, the BufferSize may be specified.
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.File
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Path   : String;
  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 the DelphiBasics.txt file
  Console.WriteLine;
  Console.WriteLine('Creating DelphiBasics.txt :');

  Path := 'DelphiBasics.txt';
  Stream := System.IO.File.&Create(Path);

  // 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
  System.IO.File.Delete(Path);

  Console.Readline;
end.
Show full unit code
  Project1.exe
  Project1.pdb
  Project1.rsp
  
  Creating DelphiBasics.txt :
  
  DelphiBasics.txt
  Project1.exe
  Project1.pdb
  Project1.rsp
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author