Xiwen
Xiwen

Reputation: 263

c# Richtext box can not load rtf file while Microsoft word using that file

i used a read stream to read an rtf file however it failed when this rtf file is opened by Microsoft word.

is there anyone know how to solve this problem?

Upvotes: 0

Views: 510

Answers (2)

SLaks
SLaks

Reputation: 887469

Like this:

new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

The proper way to read a RTF file for a rich text box (has to be of type System.Windows.Forms.RichTextBox) is like this:

myRichTextBox.LoadFile(myFilename);

But, because you have a lock on the file, you have to do it this way (credit to @slaks):

myRichTextBox.LoadFile(new FileStream(myFilename, FileAccess.Read, FileSharing.ReadWrite));

And to save it, simply call this function:

myRichTextBox.SaveFile(myFilename);

Upvotes: 1

Related Questions