Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Issue writing file in ANSI

Posted on by Microsoft Employee

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Issue writing file in ANSI

    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');

  • Suggested answer
    RE: Issue writing file in ANSI

    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.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,113 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,918 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans