Thursday, February 10, 2011

Save file with richTextBox in C Sharp

بسم الله الر حمن الر حيم

          If you work using richTextBox component in C #, you definitely will use the following method: SaveFile ();.

This method serves to store text written over the richTextBox. The Windows Forms RichTextBox control cans write the information it displays in one of installments formats:

          * Plain text

          * Unicode plain text

          * Rich-Text Format (RTF)

          * RTF with spaces in place of OLE objects

          * Plain text with a Textual representation of OLE objects

          To save the contents of the control to a file:

1. Determine the path of the file to be saved.

          To do this in a real-world application, Would you typically use the SaveFileDialog component. For an overview, see SaveFileDialog Component Overview (Windows Forms).

2. Call the SaveFile method of the RichTextBox control

          Specifying the file to save and optionally a file type. If you call the method with a file name as its only argument, the file will of be saved as RTF. To specify another file type, call the method with a value of the RichTextBoxStreamType enumeration as its second argument.

In the example below, the path set for the location of the rich-text files is the My Documents folder. This location is Used cans Because you assume That most computers running the Windows operating system include this folder earnest. Also choosing this location allows users with minimal system access levels to safely run the application. The example below assumes a form with a RichTextBox control already added.

public void SaveFile()
{
   richTextBox1.SaveFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)+ @"\Testdoc.rtf", RichTextBoxStreamType.RichNoOleObjs);
}

OR, you can use this code for specific folder:



string string123 = "nameOfFile.txt";
            richTextBox9.SaveFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer) + @"c:\MyFolder\" + string123, RichTextBoxStreamType.PlainText);

You can view on MSDN, here


Alhamdulillah, Hopefully helpful,,,

No comments:

Post a Comment