
The File object is a good way to store and retrieve text and image files to device storage.
Use the image below to refer to the form controls in the expressions for Examples 1-4.

// Check if user entered a file name.
IF efTextFileName <> ""
{
//Check if file exists.
VarFileExist = File.FileExists(efTextFileName)
IF VarFileExist == true
{
//Load file.
efTextFile = File.LoadText(efTextFileName)
efTextFileName = ""
VarOpenedFile = efTextFile
}
ELSE
{
//Display error message if file does not exists.
Variable1 = "File does not exist " & efTextFileName
Form.MessageBox("Error", Variable1, MB_OK)
efTextFileName = ""
}
}
ELSE
{
//If no file name entered, prompt user to select file.
Variable1 = Form.ShowFileOpenDialog()
efTextFile = File.LoadText(Variable1)
}
VarOpenFileName = Variable1
Form.Title = Variable1
btClose.Enable()
Note:
efTextFileName – an edit control to enter the text file name.
VarFileExist – a form variable that stores the result of the FileExists method. This is used as a condition before loading the text file or displaying an error message.
efTextFile – an edit control that contains the resulting text file from the LoadText method. Make sure that the Multi-line property is set to true.
Form.Message method – displays a message box. This method is under “Data Sources/Objects/Form”.
Form.ShowFileOpenDialog method – displays a file dialog to select the file to be opened and returns the file name as a string. This method can be found under “Data Sources/Objects/Form”. This is invoked if efTextFileName is empty.
Form.Title – a form property that can be set to any string value at run time. It can be found under “Data Sources/Form/Property”.
btClose.Enable() – a button control method that enables the control for user interaction. See “Data Sources/Objects/Controls/<button name i.e. btClose>” for details.
Bind this expression to the Action-Click property of btOpenFile button control.
IF VarOpenFileName <> "New File"
{
File.SaveText(VarOpenFileName, efTextFile)
}
ELSE
{
IF efTextFileName <> ""
{
File.SaveText(efTextFileName, efTextFile)
VarOpenFileName = efTextFileName
}
ELSE
{
IF efTextFile == ""
{
EXIT
}
ELSE
{
Variable1 = Form.ShowFileSaveDialog()
File.SaveText(Variable1, efTextFile)
VarOpenFileName = Variable1
}
}
efTextFileName = ""
}
Form.Title = VarOpenFileName
Note:
VarOpenFileName – a form variable that stores the name of the opened file.
Form.ShowFileSaveDialog method - displays a file dialog to select the file to be saved and returns the file name as a string. This method can be found under “Data Sources/Objects/Form”. This is invoked if efTextFileName is empty and efTextFile is not.
Bind this expression to the Action-Click property of btSaveFile button control.
// If its a new file, renaming not possible.
IF VarOpenFileName == "New File"
{
EXIT
}
// Prompt users for the new file name.
Variable1 = Form.ShowFileSaveDialog()
// Rename open file with the new file name.
VarBlnResult = File.RenameFile(VarOpenFileName, Variable1)
// Check if operation was successfull. Change the Form title accordingly.
IF VarBlnResult
{
VarOpenFileName = Variable1
Form.Title = VarOpenFileName
}
ELSE
{
Form.Title = VarOpenFileName
}
Note:
VarBlnResult – a boolean variable that contains the result of the RenameFile method.
Bind this expression to the Action-Click property of btRenameFile button control.
IF VarOpenFileName <> "New File"
{
// Load text file in a temporary variable for comparison.
VarOpenedFile = File.LoadText(VarOpenFileName)
}
ELSE
{
// Reset temporary variable to empty string if its a new file.
VarOpenedFile = ""
}
// Check difference between opened text file and edit control-to allow for save.
IF efTextFile <> VarOpenedFile
{
// Prompt users to save file.
VarMBResult = Form.MessageBox("Save", "Do you want to save file?", MB_YESNO)
IF IDYES == VarMBResult
{
IF VarOpenFileName == "New File"
{
// Prompt users for file name if its a new file.
VarOpenFileName = Form.ShowFileSaveDialog()
// Check if a file name was returned.
IF VarOpenFileName == ""
{
VarOpenFileName = "New File"
EXIT
}
ELSE
{
// Save is file name was entered by the user.
File.SaveText(VarOpenFileName, efTextFile)
}
}
ELSE
{
// Save if file already exists.
File.SaveText(VarOpenFileName, efTextFile)
}
}
}
// intialise all controls
efTextFile = ""
efTextFileName = ""
VarOpenFileName = "New File"
Form.Title = VarOpenFileName
btClose.Disable()
Note:
This expression checks if there was any changes made to the file and prompts the user to save the file before closing it. There really isnt any concept of file closing. It is simply an initialisation of the form controls to its default value i.e. empty string.
Refer to the Form Objects for an explanation of the MessageBox method.
Bind this expression to the Action-Click property of btClose button control.
// Prompt user for bitmap file.
Variable1 = Form.ShowFileOpenDialog()
// Load binary file to bitmap control.
image1 = File.LoadBinary(Variable1)
// Display file name in form title.
Form.Title = Variable1
Note:
Variable1 – a form variable that contains the name of the file to be opened.
image1 – an image control that accepts the result of the LoadBinary method. Displays the contents of the binary file.
// Prompt user for destination file.
Variable1 = Form.ShowFileSaveDialog()
// Save contents of the image control to the file specified.
File.SaveBinary(Variable1, image1)
// reset bitmap control.
image1 = ""
Note:
Variable1 – a form variable that contains the name of the file to be saved.
image1 – an image control that displays the binary object to be saved.
// Prompt user for source file.
Variable1 = Form.ShowFileOpenDialog()
IF Variable1 == ""
{
// If file name is empty, do nothing.
EXIT
}
// Prompt user for destination file name.
Variable2 = Form.ShowFileSaveDialog()
IF Variable2 == ""
{
// If file name is empty, do nothing.
EXIT
}
// CopyFile(Source, Destination)
VarBlnResult = File.CopyFile(Variable1, Variable2)
// Check is file copy was successful.
IF VarBlnResult
{
Form.MessageBox("Information", "File was copied successfully.", MB_OK)
}
Note:
Variable1 – a form variable that contains the name of the file to be copied.
Variable2 – a form variable that contains the new file name.
VarBlnResult – a boolean variable that contains the result of the CopyFile method.
// Check if user entered a file to delete.
IF efFileName <> ""
{
// Check if file exists.
VarFileExist = File.FileExists(efFileName)
IF VarFileExist == true
{
// Delete file.
File.DeleteFile(efFileName)
}
ELSE
{
// Display error message if file does not exists.
Form.MessageBox("Error", "Nothing to delete: File does not exist.", MB_OK)
efFileName = ""
}
}
ELSE
{
// Prompt user for file to delete.
Variable1 = Form.ShowFileOpenDialog()
// Delete file.
File.DeleteFile(Variable1)
}
Note:
efFileName – an edit control to let users enter the file to delete. If users did not enter a file name, the application prompts the user for the file to delete with the ShowFileOpenDialog method.
Variable1 = Form.ShowFileOpenDialog()
Variable2 = Form.ShowFileSaveDialog()
File.RenameFile(Variable1, Variable2)
Note:
Variable1 – a form variable that contains the name of the file to be renamed.
Variable2 – a form variable that contains the new file name.