web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Issue writing file in ANSI

(0) ShareShare
ReportReport
Posted on by

Hi all

I have an issue when writing a file. I try to write the file in textencoding::WINDOWS(Ansi), but end up having encoding "UCS-2 Little Endian" when I open the file in Notepad++

Here is my code:

LOCAL SaveToFilePath(FileContent : Text;RecordRef : RecordRef)
FieldRef := RecordRef.FIELD(3);
IF ISSERVICETIER THEN BEGIN

TempFile.CREATETEMPFILE(TEXTENCODING::Windows);
TempFile.CREATEINSTREAM(inStream);


Counter := 1;
REPEAT
Text := COPYSTR(FileContent, Counter,1);
TempFile.WRITE(Text);
Counter += 1;
UNTIL Counter > STRLEN(FileContent);

FileTo := FORMAT(FieldRef.VALUE)+'.txt';
FileName := FileTo;


// Tranfer the content from the temporary file on the NAV server to a file on the RoleTailored client.
DOWNLOADFROMSTREAM(
inStream,
'Save File to RoleTailored Client',
FileMgt.Magicpath,
'Text File *.txt| *.txt',
FileTo);
// Close the temporary file and delete it from NAV server.
TempFile.CLOSE();


END;
FileMgt.MoveFile(FileTo,ConsignorSetup."File Path"+FileName);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nareshwar Raju Vaneshwar Profile Picture
    5,596 on at

    Hi,

    I am not sure how accurate Notepad++ is.

    Check this link: programmers.stackexchange.com/.../how-to-detect-the-encoding-of-a-file

    Hope it helps.

  • Verified answer
    Community Member Profile Picture
    on at

    Thanks for the link Nareshwar!

    For those interested I rewrote the code to use .net streamwriter an encoding.default which in this case is a type of ANSI 

    FieldRef := RecordRef.FIELD(3);

    IF ISSERVICETIER THEN BEGIN

       TempFile.CREATE('C:\Temp\ConsignorTempFile.wfd');  //      

       TempFile.CREATEOUTSTREAM(outStream);        

       streamWriter := streamWriter.StreamWriter(outStream, encoding.Default);

       streamWriter.WriteLine(FileContent);

       streamWriter.Close;

       TempFile.CLOSE();

       FileTo := FORMAT(FieldRef.VALUE)+'.wfd';

       FileName := FileTo;

       DOWNLOAD(

                 'C:\Temp\temp.txt',

                 'Save File to RoleTailored Client',

                 FileMgt.Magicpath,

                 'Text File *.wfd| *.wfd',

                 FileTo);

    END;

    FileMgt.MoveFile(FileTo,ConsignorSetup."File Path"+FileName);

    ERASE('C:\Temp\ConsignorTempFile.wfd');

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

#1
HoangNam Profile Picture

HoangNam 7

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans