RichTextBox.LoadFile 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將檔案內容載入控制項 RichTextBox 。
多載
| 名稱 | Description |
|---|---|
| LoadFile(String) |
將富文字格式(RTF)或標準 ASCII 文字檔載入控制器。RichTextBox |
| LoadFile(Stream, RichTextBoxStreamType) |
將現有資料流的內容載入控制項。RichTextBox |
| LoadFile(String, RichTextBoxStreamType) |
將特定類型的檔案載入控制 RichTextBox 項。 |
LoadFile(String)
將富文字格式(RTF)或標準 ASCII 文字檔載入控制器。RichTextBox
public:
void LoadFile(System::String ^ path);
public void LoadFile(string path);
member this.LoadFile : string -> unit
Public Sub LoadFile (path As String)
參數
- path
- String
檔案名稱和要載入控制項的位置。
例外狀況
載入控制碼時發生錯誤。
被載入的檔案不是 RTF 文件。
範例
以下程式碼範例將 RTF 檔案開啟進入控制項。RichTextBox 範例中使用 該 OpenFileDialog 類別來顯示一個對話框,向使用者請求檔案。 程式碼接著載入該檔案,假設它是 RTF 文件檔。 如果檔案沒有例外,範例程式碼會拋出例外。 此範例要求程式碼置 Form 於一個名為 RichTextBox 的控制 richTextBox1項類別中。
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 );
}
}
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);
}
}
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
備註
使用該 LoadFile 方法載入檔案時,所載入檔案的內容會取代控制項的 RichTextBox 全部內容。 這會導致 和 TextRtf 屬性的值改變。 你可以用這個方法將先前建立的文字或 RTF 文件載入控制項進行操作。 如果你想儲存檔案,可以用這個 SaveFile 方法。
Note
在此版本 LoadFile 中,若載入的檔案不是 RTF 文件,則會發生例外。 若要載入不同類型的檔案,如 ASCII 文字檔,請使用接受列舉數值 RichTextBoxStreamType 作為參數的其他版本。
Note
LoadFile該方法在建立 RichTextBoxhandle 之前不會開啟檔案。 在呼叫方法 LoadFile 前,請確保控制項的 handle 已經建立。
另請參閱
適用於
LoadFile(Stream, RichTextBoxStreamType)
將現有資料流的內容載入控制項。RichTextBox
public:
void LoadFile(System::IO::Stream ^ data, System::Windows::Forms::RichTextBoxStreamType fileType);
public void LoadFile(System.IO.Stream data, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.LoadFile : System.IO.Stream * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub LoadFile (data As Stream, fileType As RichTextBoxStreamType)
參數
- data
- Stream
一串資料要載入控制系統 RichTextBox 。
- fileType
- RichTextBoxStreamType
這是其中一項 RichTextBoxStreamType 價值。
例外狀況
載入控制碼時發生錯誤。
.NET 5 及以後版本:參數 data 為 null。
被載入的檔案不是 RTF 文件。
範例
以下程式碼範例示範使用 SaveFile and LoadFile 方法搭配串流。 同時也示範使用 FileDialog.FileName、 FileDialog.DefaultExt、 SaveFileDialog.CreatePrompt和 SaveFileDialog.OverwritePrompt 成員。
這是一個完整的範例,當你複製到專案時,就能立即執行。
using namespace System;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Windows::Forms;
public ref class Form1: public Form
{
public private:
RichTextBox^ RichTextBox1;
Button^ Button1;
RichTextBox^ RichTextBox2;
Button^ Button2;
SaveFileDialog^ SaveFileDialog1;
public:
Form1()
: Form()
{
userInput = gcnew MemoryStream;
this->RichTextBox1 = gcnew RichTextBox;
this->Button1 = gcnew Button;
this->RichTextBox2 = gcnew RichTextBox;
this->Button2 = gcnew Button;
this->SaveFileDialog1 = gcnew SaveFileDialog;
this->SuspendLayout();
this->RichTextBox1->Location = Point( 24, 64 );
this->RichTextBox1->Name = "RichTextBox1";
this->RichTextBox1->TabIndex = 0;
this->RichTextBox1->Text = "Type something here.";
this->Button1->Location = Point( 96, 16 );
this->Button1->Name = "Button1";
this->Button1->Size = Size( 96, 24 );
this->Button1->TabIndex = 1;
this->Button1->Text = "Save To Stream";
this->Button1->Click +=
gcnew EventHandler( this, &Form1::Button1_Click );
this->RichTextBox2->Location = Point( 152, 64 );
this->RichTextBox2->Name = "RichTextBox2";
this->RichTextBox2->TabIndex = 3;
this->RichTextBox2->Text = "It will be added to the stream "
"and appear here.";
this->Button2->Location = Point( 104, 200 );
this->Button2->Name = "Button2";
this->Button2->Size = Size( 88, 32 );
this->Button2->TabIndex = 4;
this->Button2->Text = "Save Stream To File";
this->Button2->Click +=
gcnew EventHandler( this, &Form1::Button2_Click );
this->ClientSize = Size( 292, 266 );
this->Controls->Add( this->Button2 );
this->Controls->Add( this->RichTextBox2 );
this->Controls->Add( this->Button1 );
this->Controls->Add( this->RichTextBox1 );
this->Name = "Form1";
this->Text = "Form1";
this->ResumeLayout( false );
}
// Declare a new memory stream.
MemoryStream^ userInput;
private:
// Save the content of RichTextBox1 to the memory stream,
// appending a LineFeed character.
void Button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
RichTextBox1->SaveFile( userInput, RichTextBoxStreamType::PlainText );
userInput->WriteByte( 13 );
// Display the entire contents of the stream,
// by setting its position to 0, to RichTextBox2.
userInput->Position = 0;
RichTextBox2->LoadFile( userInput, RichTextBoxStreamType::PlainText );
}
// Shows the use of a SaveFileDialog to save a MemoryStream to a file.
void Button2_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
// Set the properties on SaveFileDialog1 so the user is
// prompted to create the file if it doesn't exist
// or overwrite the file if it does exist.
SaveFileDialog1->CreatePrompt = true;
SaveFileDialog1->OverwritePrompt = true;
// Set the file name to myText.txt, set the type filter
// to text files, and set the initial directory to the
// MyDocuments folder.
SaveFileDialog1->FileName = "myText";
// DefaultExt is only used when "All files" is selected from
// the filter box and no extension is specified by the user.
SaveFileDialog1->DefaultExt = "txt";
SaveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
SaveFileDialog1->InitialDirectory =
Environment->GetFolderPath(Environment::SpecialFolder::MyDocuments);
// Call ShowDialog and check for a return value of DialogResult.OK,
// which indicates that the file was saved.
DialogResult result = SaveFileDialog1->ShowDialog();
Stream^ fileStream;
if ( result == DialogResult::OK )
{
fileStream = SaveFileDialog1->OpenFile();
userInput->Position = 0;
userInput->WriteTo( fileStream );
fileStream->Close();
}
}
};
int main()
{
Application::Run( gcnew Form1 );
}
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public partial class Form1: Form
{
internal RichTextBox RichTextBox1;
internal Button Button1;
internal RichTextBox RichTextBox2;
internal Button Button2;
internal SaveFileDialog SaveFileDialog1;
public Form1() : base()
{
this.RichTextBox1 = new RichTextBox();
this.Button1 = new Button();
this.RichTextBox2 = new RichTextBox();
this.Button2 = new Button();
this.SaveFileDialog1 = new SaveFileDialog();
this.SuspendLayout();
this.RichTextBox1.Location = new Point(24, 64);
this.RichTextBox1.Name = "RichTextBox1";
this.RichTextBox1.TabIndex = 0;
this.RichTextBox1.Text = "Type something here.";
this.Button1.Location = new Point(96, 16);
this.Button1.Name = "Button1";
this.Button1.Size = new Size(96, 24);
this.Button1.TabIndex = 1;
this.Button1.Text = "Save To Stream";
this.Button1.Click += new EventHandler(Button1_Click);
this.RichTextBox2.Location = new Point(152, 64);
this.RichTextBox2.Name = "RichTextBox2";
this.RichTextBox2.TabIndex = 3;
this.RichTextBox2.Text =
"It will be added to the stream and appear here.";
this.Button2.Location = new Point(104, 200);
this.Button2.Name = "Button2";
this.Button2.Size = new Size(88, 32);
this.Button2.TabIndex = 4;
this.Button2.Text = "Save Stream To File";
this.Button2.Click += new EventHandler(Button2_Click);
this.ClientSize = new Size(292, 266);
this.Controls.Add(this.Button2);
this.Controls.Add(this.RichTextBox2);
this.Controls.Add(this.Button1);
this.Controls.Add(this.RichTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
public static void Main()
{
Application.Run(new Form1());
}
// Declare a new memory stream.
MemoryStream userInput = new MemoryStream();
// Save the content of RichTextBox1 to the memory stream,
// appending a LineFeed character.
private void Button1_Click(Object sender, EventArgs e)
{
RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText);
userInput.WriteByte(13);
// Display the entire contents of the stream,
// by setting its position to 0, to RichTextBox2.
userInput.Position = 0;
RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText);
}
// Shows the use of a SaveFileDialog to save a MemoryStream to a file.
private void Button2_Click(Object sender, EventArgs e)
{
// Set the properties on SaveFileDialog1 so the user is
// prompted to create the file if it doesn't exist
// or overwrite the file if it does exist.
SaveFileDialog1.CreatePrompt = true;
SaveFileDialog1.OverwritePrompt = true;
// Set the file name to myText.txt, set the type filter
// to text files, and set the initial directory to the
// MyDocuments folder.
SaveFileDialog1.FileName = "myText";
// DefaultExt is only used when "All files" is selected from
// the filter box and no extension is specified by the user.
SaveFileDialog1.DefaultExt = "txt";
SaveFileDialog1.Filter =
"Text files (*.txt)|*.txt|All files (*.*)|*.*";
SaveFileDialog1.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Call ShowDialog and check for a return value of DialogResult.OK,
// which indicates that the file was saved.
DialogResult result = SaveFileDialog1.ShowDialog();
Stream fileStream;
if (result == DialogResult.OK)
{
// Open the file, copy the contents of memoryStream to fileStream,
// and close fileStream. Set the memoryStream.Position value to 0
// to copy the entire stream.
fileStream = SaveFileDialog1.OpenFile();
userInput.Position = 0;
userInput.WriteTo(fileStream);
fileStream.Close();
}
}
}
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Partial Public Class Form1
Inherits Form
Friend WithEvents RichTextBox1 As RichTextBox
Friend WithEvents Button1 As Button
Friend WithEvents RichTextBox2 As RichTextBox
Friend WithEvents Button2 As Button
Friend WithEvents SaveFileDialog1 As SaveFileDialog
Public Sub New()
MyBase.New()
Me.RichTextBox1 = New RichTextBox
Me.Button1 = New Button
Me.RichTextBox2 = New RichTextBox
Me.Button2 = New Button
Me.SaveFileDialog1 = New SaveFileDialog
Me.SuspendLayout()
Me.RichTextBox1.Location = New Point(24, 64)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "Type something here."
Me.Button1.Location = New Point(96, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New Size(96, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Save To Stream"
Me.RichTextBox2.Location = New Point(152, 64)
Me.RichTextBox2.Name = "RichTextBox2"
Me.RichTextBox2.TabIndex = 3
Me.RichTextBox2.Text = "It will be added to the stream and appear here."
Me.Button2.Location = New Point(104, 200)
Me.Button2.Name = "Button2"
Me.Button2.Size = New Size(88, 32)
Me.Button2.TabIndex = 4
Me.Button2.Text = "Save Stream To File"
Me.ClientSize = New Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.RichTextBox2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Public Shared Sub Main()
Application.Run(New Form1)
End Sub
' Declare a new memory stream.
Dim userInput As New MemoryStream
' Save the content of RichTextBox1 to the memory stream, appending
'a LineFeed character.
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText)
userInput.WriteByte(13)
' Display the entire contents of the stream,
' by setting its position to 0, to RichTextBox2.
userInput.Position = 0
RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText)
End Sub
' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button2.Click
' Set the properties on SaveFileDialog1 so the user is
' prompted to create the file if it doesn't exist
' or overwrite the file if it does exist.
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.OverwritePrompt = True
' Set the file name to myText.txt, set the type filter
' to text files, and set the initial directory to the
' MyDocuments folder.
SaveFileDialog1.FileName = "myText"
' DefaultExt is only used when "All files" is selected from
' the filter box and no extension is specified by the user.
SaveFileDialog1.DefaultExt = "txt"
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.InitialDirectory = _
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
' Call ShowDialog and check for a return value of DialogResult.OK,
' which indicates that the file was saved.
Dim result As DialogResult = SaveFileDialog1.ShowDialog()
Dim fileStream As Stream
If (result = DialogResult.OK) Then
' Open the file, copy the contents of memoryStream to fileStream,
' and close fileStream. Set the memoryStream.Position value to 0 to
' copy the entire stream.
fileStream = SaveFileDialog1.OpenFile()
userInput.Position = 0
userInput.WriteTo(fileStream)
fileStream.Close()
End If
End Sub
End Class
備註
你可以用這個版本 LoadFile 的方法,從現有的資料流載入資料 RichTextBox 。 載入控制項的資料會取代控制項的 RichTextBox 全部內容。 這會導致 和 TextRtf 屬性的值改變。 你可以用這個方法將先前已開啟的檔案載入資料串流,並進入控制項進行操作。 如果你想把控制項的內容存回串流,可以使用 SaveFile 接受 Stream 物件作為參數的方法。
此版本 LoadFile 的方法也能指定將載入控制的資料類型。 此功能允許您將包含非富文本格式(RTF)文件的資料串流納入控制項。
Note
LoadFile該方法在建立 RichTextBoxhandle 之前不會開啟檔案。 在呼叫方法 LoadFile 前,請確保控制項的 handle 已經建立。
另請參閱
適用於
LoadFile(String, RichTextBoxStreamType)
將特定類型的檔案載入控制 RichTextBox 項。
public:
void LoadFile(System::String ^ path, System::Windows::Forms::RichTextBoxStreamType fileType);
public void LoadFile(string path, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.LoadFile : string * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub LoadFile (path As String, fileType As RichTextBoxStreamType)
參數
- path
- String
檔案名稱和要載入控制項的位置。
- fileType
- RichTextBoxStreamType
這是其中一項 RichTextBoxStreamType 價值。
例外狀況
載入控制碼時發生錯誤。
被載入的檔案不是 RTF 文件。
範例
以下程式碼範例會將文字檔打開進入 RichTextBox 控制項。 範例中使用 該 OpenFileDialog 類別來顯示一個對話框,向使用者請求檔案。 程式碼接著將該檔案載入 RichTextBox 控制項。 範例中使用此版本 LoadFile 的方法,指定檔案以 ASCII 文字檔開啟,而非標準富文字格式。 此範例要求程式碼置 Form 於一個名為 RichTextBox 的控制 richTextBox1項類別中。
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, RichTextBoxStreamType::PlainText );
}
}
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, RichTextBoxStreamType.PlainText);
}
}
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, _
RichTextBoxStreamType.PlainText)
End If
End Sub
備註
使用該 LoadFile 方法載入檔案時,所載入檔案的內容會取代控制項的 RichTextBox 全部內容。 這會導致 和 TextRtf 屬性的值改變。 你可以用這個方法將先前建立的文字或富文本格式(RTF)文件載入操作控制。 如果你想儲存檔案,可以用這個 SaveFile 方法。
你可以用這個版本 LoadFile 的方法來指定載入檔案的檔案類型。 此功能允許您將 RTF 文件以外的檔案載入控制面板。
Note
LoadFile該方法在建立 RichTextBoxhandle 之前不會開啟檔案。 在呼叫方法 LoadFile 前,請確保控制項的 handle 已經建立。