Temp file not found after closing

Hi everyone,

I created a temp file on the server, wrote some information on it, and then wanted to transfer the data I wrote to another file in the client side.
Here is the piece of code I wrote:

LogFile.CREATETEMPFILE();
LogFile.TEXTMODE(TRUE);
FileName := LogFile.NAME;
LogFile.WRITE('some info');
LogFile.CLOSE;
LogFile.OPEN(FileName);
cdu419.AppendAllTextToClientFile(FileName,rSalesSetup."Post & Print Logfile Path"+rSalesSetup."Post & Print Logfile Name"+'.txt');

I got the following error message:

This message is for C/AL programmers: A call to System.IO.File.ReadAllText failed with this message: Could not find file 'C:\ProgramData\Microsoft\Microsoft Dynamics NAV\70\Server\ddd\users\eee\www\TEMP\__TEMP__73abda84b2ed4d91933c5772f0dfb1da.tmp'.

__TEMP__73abda84b2ed4d91933c5772f0dfb1da.tmp is the name of the temp file I created (I checked).
What is going wrong? Is the file deleted when LogFile.CLOSE is executed?

Thanks in advance :mrgreen:

Hi,

I checked the help content of the CREATETEMPFILE function and the file is deleted:

Microsoft Dynamics NAV Documentation

CREATETEMPFILE Function (File)

Creates a temporary file. This enables you to save data of any format to a temporary file. This file has a unique name and will be stored in a temporary file folder.

File.CreateTempFile

Parameters

File

Type: File

Use this variable to refer to the file.

You can use this function with NAME Function (File) and CLOSE Function (File).

This function is not supported in the RoleTailored client.

This example creates a temporary file with the text Hello and then deletes the file by using the File.CLOSE function. This example requires that you create the following variable.

Variable Data type

FileName

File

FileName.CREATETEMPFILE:

FileName.WRITE(‘Hello’);

FileName.CLOSE;

I will share with you some code I use to treat temporary files:

1. first create the file

You will need the following variables:

FilePath, type Text(250)

FileName, type Text(30)

Filesystem, type Record, subtype File

TextFile, type File

FilePath := TEMPORARYPATH; // check TEMPORARYPATH reference

Filesystem.Path := ‘’;

Filesystem.“Is a file” := TRUE;

Filesystem.Name := ‘LogFile.txt’;

FilePath := FilePath + Filesystem.Name;

// If you want you can check if the file already exists and deal with it: abort or overwrite the file.

TextFile.TEXTMODE(TRUE);

TextFile.WRITEMODE(TRUE);

TextFile.CREATE(FilePath);

2. Insert data:

TextFile.WRITE(‘some info I would like to share with you’);

3. Close file and display data

You will need the following variable:

ShellObject, type Automation, subtype ‘Microsoft Shell Controls And Automation’.Shell

TextFile.CLOSE;

CREATE(ShellObject, TRUE, TRUE); // check CREATE Function (Automation) reference

ShellObject.Open(FilePath); // this will use the system default viewer for the file type. Usually for txt files is notepad

// if it is csv file probably will open with MS Excel

Best Regards,

Jose.