Editar

Partilhar via


File.Exists(Text) Method

Version: Available or changed with runtime version 1.0.

Determines whether a file exists.

Note

This method is supported only in Business Central on-premises.

Syntax

[Ok := ]  File.Exists(Name: Text)

Note

This method can be invoked without specifying the data type name.

Parameters

Name
 Type: Text
The name of the file that you want to check. This includes the path. When you enter the path, consider these shortcuts:

  • You can omit the drive designation if the file is located on the current drive.
  • You can omit the full path if the file is located in the current directory.
  • You can enter only the subdirectory name if the file is located in a subdirectory of the current directory.

Return Value

[Optional] Ok
 Type: Boolean
true if the server instance has access to the file; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Example

The following example uses the Exists method to determine whether the specified file exists. If the file exists, then the WriteMode Method (File) allows the file to be open for writing. The Open Method (File) opens the file, the Write Method (File) writes the text “Hello World” to the file, and then the Close Method (File) method closes the file. If the file does not exist, an error message is displayed. This example assumes that you have created the following file C:\TestFolder\TestFile2.txt.

 var
    TestFile: File;
    FileName: Text;
begin
    FileName := 'C:\TestFolder\TestFile2.txt';  
    if exists(FileName) then begin  
      TestFile.WriteMode(true);  
      TestFile.Open(FileName);  
      TestFile.Write('Hello World');  
      TestFile.Close;  
    end else  
        Message('%1 does not exist.', FileName);  
end;

File Data Type
Get Started with AL
Developing Extensions