In This Article

Loading and Saving Files

It's very easy to load document text from a file, stream, or string. Likewise, document text can be saved back out to a file, stream, or string.

Loading Text Directly from a File/Stream

Document text may be loaded from files using one of the overloads of the ITextDocument.LoadFile method.

Use of a LoadFile method will reset the IsModified property to false and will clear the undo history. Use of the LoadFile method overloads that accept a path parameter will auto-update the FileName property with the file path.

Load from File System Using UTF-8 Encoding

The first overload of LoadFile simply accepts a path parameter. It loads files using standard UTF-8 encoding.

This code loads text from a file using UTF-8 encoding:

document.LoadFile(@"C:\Code.cs");

Load from File System Using a Specified Encoding

The second overload of LoadFile accepts a path parameter as well as an Encoding parameter. It loads files using the Encoding that you specify. This allows for easy loading of non-UTF-8 encoded files.

This code loads text from a file using ASCII encoding:

document.LoadFile(@"C:\Code.cs", Encoding.ASCII);

Load from Stream Using a Specified Encoding

The third overload accepts a Stream parameter as well as an Encoding parameter. It loads from the stream using the Encoding that you specify. This allows for easy loading of non-UTF-8 encoded files.

This code loads text from a Stream using UTF-8 encoding:

document.LoadFile(stream, Encoding.UTF8);

Loading Text from a String

Sometimes a document's text is fetched using an external process, such as from a database. In this case, the text to place in the document is available as a string variable.

The ITextDocument.SetText method can be used to completely replace the document text with a string.

There are several overloads of the SetText available. The overload that only accepts a String parameter will reset the IsModified property to false and will clear the undo history. The other overloads will not do this.

This code replaces the text content of a document from a string, marks the IsModified property to false, and clears the undo history:

string text = "int a;";
document.SetText(text);

This code replaces the text content of a document from a string, without updating the IsModified property or the undo history:

string text = "int a;";
document.SetText(TextChangeTypes.Typing, text);

Saving Text to a File

Document text may be saved to files using one of the overloads of the ITextDocument.SaveFile method.

Save to File Using UTI-8 Encoding

The first overload simply accepts a path parameter and a LineTerminator parameter. It saves files using standard UTF-8 encoding.

This code saves document text to a file using UTF-8 encoding:

document.SaveFile("C:\Code.cs", LineTerminator.CarriageReturnNewline);

Save to File Using a Specified Encoding

The second overload accepts a path parameter, a LineTerminator parameter, as well as an Encoding parameter. It saves files using the Encoding that you specify. This allows for easy saving of non-UTF-8 encoded files.

This code saves document text to a file using ASCII encoding:

document.SaveFile("C:\Code.cs", Encoding.ASCII, LineTerminator.CarriageReturnNewline);

Save to Stream Using a Specified Encoding

The third overload accepts a Stream parameter, a LineTerminator parameter, as well as an Encoding parameter. It saves to the stream using the Encoding that you specify. This allows for easy saving of non-UTF-8 encoded files.

This code saves document text to a Stream using UTF-8 encoding:

document.SaveFile(stream, Encoding.UTF8, LineTerminator.CarriageReturnNewline);

Updating the IsModified Property

After saving a document, it is a good idea to mark it as no longer modified. Save points are tracked in the undo history so that you are notified when the document reaches the save point.

This code sets a save point by telling a document that it is no longer modified:

document.IsModified = false;

When the ITextDocument.IsModified property is set to false all unsaved changes in undo history's change tracking will be switched to saved changes.

Calling any of the SaveFile overloads with a path parameter will automatically set the ITextDocument.IsModified property to false if the specified path is the same as the document's FileName property value.

Getting Text into a String

Sometimes a document's text needs to be saved using an external process, such as to a database. In this case, the text to save needs to be retrieve into a string variable.

The current snapshot of a document (see the Documents, Snapshots, and Versions topic for more information) is available via the ITextDocument.CurrentSnapshot property. The ITextSnapshot.GetText method can be used to retrieve a snapshot's text content with a specified LineTerminator. The Text property returns the snapshot's text using standard Windows carriage return / newline line terminators.

This code fetches the current document text into a string:

string text = document.CurrentSnapshot.Text;

Exporting to HTML / RTF

The text framework supports exporting document text to HTML and RTF formats.

See the Exporting to HTML / RTF topic for more information on this feature.

Storing the File Name of a Document

The ITextDocument.FileName property can be used to store the file name (full path) of the file that has been loaded into the document. The LoadFile method overloads that read a file automatically set this property.

When the FileName property value is changed, the FileNameChanged event is raised.