Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
RichTextBox Class
LoadFile Method
 LoadFile Method (String)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
RichTextBox..::.LoadFile Method (String)

Loads a rich text format (RTF) or standard ASCII text file into the RichTextBox control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Sub LoadFile ( _
    path As String _
)
Visual Basic (Usage)
Dim instance As RichTextBox
Dim path As String

instance.LoadFile(path)
C#
public void LoadFile(
    string path
)
Visual C++
public:
void LoadFile(
    String^ path
)
JScript
public function LoadFile(
    path : String
)

Parameters

path
Type: System..::.String
The name and location of the file to load into the control.
ExceptionCondition
IOException

An error occurred while loading the file into the control.

ArgumentException

The file being loaded is not an RTF document.

When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. This will cause the values of the Text and Rtf properties to change. You can use this method to load a previously created text or RTF document into the control for manipulation. If you want to save the file, you can use the SaveFile method.

NoteNote:

With this version of the LoadFile method, if the file being loaded is not an RTF document, an exception will occur. To load a different type of file such as an ASCII text file, use the other versions of this method that accept a value from the RichTextBoxStreamType enumeration as a parameter.

NoteNote:

The LoadFile method will not open a file until a handle is created for the RichTextBox. Ensure that the control's handle is created before calling the LoadFile method.

The following code example opens an RTF file into the RichTextBox control. The example uses the OpenFileDialog class to display a dialog to request the file from the user. The code then loads that file assuming that it is an RTF document file. If the file is not, the example code will throw an exception. This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

Visual Basic
Public Sub LoadMyFile()
    ' Create an OpenFileDialog to request a file to open.
    Dim openFile1 As New OpenFileDialog()

    ' Initialize the OpenFileDialog to look for RTF files.
    openFile1.DefaultExt = "*.rtf"
    openFile1.Filter = "RTF Files|*.rtf"

    ' Determine whether the user selected a file from the OpenFileDialog.
    If (openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
        And (openFile1.FileName.Length > 0) Then

        ' Load the contents of the file into the RichTextBox.
        richTextBox1.LoadFile(openFile1.FileName)
    End If
End Sub


C#
public void LoadMyFile()
{
   // Create an OpenFileDialog to request a file to open.
   OpenFileDialog openFile1 = new OpenFileDialog();

   // Initialize the OpenFileDialog to look for RTF files.
   openFile1.DefaultExt = "*.rtf";
   openFile1.Filter = "RTF Files|*.rtf";

   // Determine whether the user selected a file from the OpenFileDialog.
   if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
      openFile1.FileName.Length > 0) 
   {
      // Load the contents of the file into the RichTextBox.
      richTextBox1.LoadFile(openFile1.FileName);
   }
}


Visual C++
public:
   void LoadMyFile()
   {
      // Create an OpenFileDialog to request a file to open.
      OpenFileDialog^ openFile1 = gcnew OpenFileDialog;

      // Initialize the OpenFileDialog to look for RTF files.
      openFile1->DefaultExt = "*.rtf";
      openFile1->Filter = "RTF Files|*.rtf";

      // Determine whether the user selected a file from the OpenFileDialog.
      if ( openFile1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
         openFile1->FileName->Length > 0 )
      {
         // Load the contents of the file into the RichTextBox.
         richTextBox1->LoadFile( openFile1->FileName );
      }
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker