用 C# 實作一個簡單的 Word – LoadFile, SaveFile

Author: Eric  //  Category: C Sharp

01. RichTextBox 除了之前介紹的 Copy、Cut、Paste、SelectAll 等 Method 外,還有 Clear、ClearUndo、Find、LoadFile、Redo、Undo、SaveFile ...etc。這篇就來介紹 LoadFile、SaveFile 開檔、存檔。

// 完整的 RichTextBox 請看 MSDN RichTextBox Class

02. 男主角 :LoadFile 它有三個好兄弟。

  a. LoadFile( String ) : Loads a rich text format (RTF) or standard ASCII text file into the RichTextBox control.
// If the file being loaded is not an RTF document, an exception will occur.

richTextBox.LoadFile( fileName );

  b. LoadFile( String, RichTextBoxStreamType ) : Loads a specific type of file into the RichTextBox control.

// for Rich Text File( RTF )
richTextBox.LoadFile( fileName, RichTextBoxStreamType.RichText );
// for Plain Text File( Txt )
richTextBox.LoadFile( fileName, RichTextBoxStreamType.PlainText );

  c. LoadFile( Stream, RichTextBoxStreamType ) : Loads the contents of an existing data stream into the RichTextBox control.

// Declare a new memory stream.
System.IO.MemoryStream myMemory = new System.IO.MemoryStream();

private void button_Click( object sender , EventArgs e ) {
  // 把 leftRichTextBox 的內容存到 memory stream
  leftRichTextBox.SaveFile( myMemory , RichTextBoxStreamType.PlainText );

  myMemory.WriteByte( 13 ); // ASCII 13 (CR)
  myMemory.WriteByte( 10 ); // ASCII 10 (LF)
  myMemory.Position = 0;

  // 把 memory stream 的內容讀到 leftRichTextBox
  rightRichTextBox.LoadFile( myMemory , RichTextBoxStreamType.PlainText );
} // button_Click()

03. 女主角 :SaveFile 它有三個好姊妹。

  a. SaveFile(String) Saves the contents of the RichTextBox to a rich text format (RTF) file.

  b. SaveFile(String, RichTextBoxStreamType) Saves the contents of the RichTextBox to a specific type of file.

  c. SaveFile(Stream, RichTextBoxStreamType) Saves the contents of a RichTextBox control to an open data stream.

// 用法同剛才介紹的 LoadFile

04. 那如何把它加到之前做的 HelloWord Editor 呢?為了公平起見,所以現在介紹存檔的另存新檔。

  a. 跟之前的 Copy、Cut、Paste、SelectAll 一樣,先在 NewFileForm.cs 寫好一些方法以供呼叫 RTBLoadOldFile、RTBSaveFileAs。

  b. 先來一個 SaveFileDialog,看要不要設定一下 InitialDirectory 、Filter 等,如果按了確定就存檔。

private void SaveAsToolStripMenuItem_Click( object sender , EventArgs e ) {
  NewFileForm activeForm = ( NewFileForm ) this.ActiveMdiChild;
  if ( activeForm != null ) {
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    if ( saveFileDialog.ShowDialog( this ) == DialogResult.OK ) {
      string fileName = saveFileDialog.FileName;
      activeForm.RTBSaveFileAs( fileName );
    } // if
  } // if 避免有人來亂, 沒有視窗卻要存檔
  else
    MessageBox.Show( "來亂的喔!!" );
} // SaveAsToolStripMenuItem_Click() 另存新檔

上面也可以設定 InitialDirectory, Filter, DefaultExt, AddExtension, RestoreDirectory ... etc,更多請看 MSDN SaveFileDialog Class

saveFileDialog.InitialDirectory = Environment.GetFolderPath( Environment.SpecialFolder.Personal );
saveFileDialog.Filter = "HelloWord Files (*.hw)|*.hw|Rich Text Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

// Environment.SpecialFolder Enumeration>

上面 Environment.SpecialFolder.Personal 的 Personal 可以換成 Desktop、MyComputer、MyDocuments ...etc。或直接寫路徑 saveFileDialog.InitialDirectory = "c:\\";

05. 至於 NewFileForm.cs 裡的 RTBLoadOldFile 和 RTBSaveFileAs 要怎麼寫就自己思考下吧。

Tags: , , , ,

One Response to “用 C# 實作一個簡單的 Word – LoadFile, SaveFile”

  1. Ming Says:

    您好,可以跟您要一下你部落格的滑鼠圖案嘛?
    感謝@@

Leave a Reply