System.SysUtils.FileCreate

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function FileCreate(const FileName: string): THandle;
function FileCreate(const FileName: string; Rights: Integer): THandle;
function FileCreate(const FileName: string; Mode: LongWord; Rights: Integer): THandle;

C++

extern DELPHI_PACKAGE NativeUInt __fastcall FileCreate(const System::UnicodeString FileName)/* overload */;

Properties

Type Visibility Source Unit Parent
function public
System.SysUtils.pas
System.SysUtils.hpp
System.SysUtils System.SysUtils

Description

Creates a new file.

FileCreate creates a new file with the specified name. If the return value is not INVALID_HANDLE_VALUE, the function was successful and the value is the file handle of the new file. A return value of INVALID_HANDLE_VALUE indicates that an error occurred.

The return type for FileCreate since Delphi 2010 has changed from an Integer type to THandle.

 var
   MyFile: THandle;
 begin
   MyFile := FileCreate('C:\temp\bla.txt');
   if MyFile = INVALID_HANDLE_VALUE then
     raise Exception.Create('File already exists');
 end;

To emulate the previous behavior, you could cast the FileCreate return value into a NativeInt, and then a return value of -1 indicates an error. Here is a code snippet demonstrating how to do this.

var
  MyFile: Integer; // for 32bit platform.
begin
  MyFile := NativeInt(FileCreate('C:\temp\bla.txt'));
  if MyFile = -1 then
    raise Exception.Create('File already exists');
end;

Note: On Windows, the FileAccessRights variable and Rights parameter are ignored.

See Also

Code Examples