Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Issue writing file in ANSI

(0) ShareShare
ReportReport
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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans