Document.SaveFile(stream...) is not overwriting the stream content.

SyntaxEditor for WPF Forum

Posted 2 years ago by Farouk Bouali
Version: 22.1.0
Avatar

Hi Actipro, used version 19.1.687.0

This mehtod Document.SaveFile(Stream stream, Encoding encoding, LineTerminator lineTerminator) is appending the new content to the stream not overwriting the stream. 

Steps to reproduce my issue:

- I have file.txt, the content of file is (test test test test). I am creating a stream from that file. stream (Lenght: 19, Position:19).

- I am loading the created stream using Document.LoadFile(Stream stream,...). The content (test test test test) is displayed, all good.

- In the EditorDocument, I change the content to (actipro), and call Document.SaveFile(stream,...), the passed stream is the same instance of the loaded stream. 

- After calling the SaveFile method, the stream changed to (Lenght: 26, position:26). the new content is (test test test testactipro). 

Is this expected? 

Comments (1)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The SaveFile overload where you pass it a Stream has a simple implementation like this:

StreamWriter writer = new StreamWriter(stream, encoding);
writer.Write(text); 
writer.Flush(); 

It's going to write out the document text right where the current Stream position is.  In your case, it sounds like the Stream's position is after the four "test" words, so the result is correct per what you are doing.

If you want to overwrite a file, you might want to use one of the SaveFile overloads that take a file path instead.  Those will create a new file each time, making it completely overwrite the previous file content.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.