Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a RichTextBox and a Button. 3

This is my code to save the text that was written.
C#
richTextBox1.SaveFile(@"c:/users/hp/desktop/Chat.txt");
richTextBox1.Clear();


I write in the richtextbox but when I click on the Button it doesn't add my new text to previous text file.

Any Idea?
Posted
Updated 3-Nov-10 22:58pm
v2
Comments
Dalek Dave 4-Nov-10 4:58am    
Edited for Grammar and Syntax.

Everyone here seems to be missing the point that this is a RichTextBox!

I am assuming that you want to preserve the formatting information that can be entered into the RichTextBox.

Others are also making it way more complex than it needs to be.

Basically, you want to append the information to the file. But, you want to write the RichText, not the Text.

So, the RichTextBox provides a property called Rtf that is the text with the rich text formatting.

If you want to take the original file and append it, just open a stream writer using File.AppendText and then write the text...like so:

C#
using (StreamWriter writer = File.AppendText(@"c:\teste\chat.txt"))
{
  writer.Write(richTextBox1.Rtf);
}


it's that simple...
 
Share this answer
 
There is no Overload available in richtextbox.SaveFile for File appending...


Use the below code to slove your Problem

C#
if (File.Exists("c:\\temp.txt"))
 {
     byte[] FileBuf = File.ReadAllBytes("c:\\temp.txt");
     byte[] RichBuf = Encoding.ASCII.GetBytes(richTextBox1.Text);
     byte[] Total = new byte[FileBuf.Length + RichBuf.Length];
     System.Buffer.BlockCopy(FileBuf, 0, Total, 0, FileBuf.Length);
     System.Buffer.BlockCopy(RichBuf, 0 , Total, FileBuf.Length , RichBuf.Length);
     File.WriteAllBytes("c:\\temp.txt", Total);
 }
 
Share this answer
 
Comments
William Winner 5-Nov-10 15:38pm    
this does work, but you're forgetting a very important part of the RichTextBox...the formatting information.

Instead of
byte[] RichBuf = Encoding.ASCII.GetBytes(richTextBox1.Text);
use:
byte[] RichBuf = Encoding.ASCII.GetBytes(richTextBox1.Rtf);

and then you'll preserve the formatting
hi,

try this

richTextBox1.SaveFile("c:\\users\\hp\\desktop\\Chat.txt", RichTextBoxStreamType.RichText)

jerem
 
Share this answer
 
Comments
pampam110 4-Nov-10 4:52am    
thanks. but i guess i didn't mention what i want.
i write something in the richtextbox and then by click the button, it saves to "Chat.txt"
then i clear the text, and write new sentences... i want to save new sentences + old sentences in "Chat.txt"
can you guide me about this problem...
thanks again.
Rajesh Anuhya 4-Nov-10 8:07am    
This is not the OP's Requirement....
Use this to find the end and insert an informations

C#
using System;
using System.IO;
class DirAppend
{
    public static void Main(String[] args)
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            Log ("Test1", w);
            Log ("Test2", w);
            // Close the writer and underlying file.
            w.Close();
        }
        // Open and read the file.
        using (StreamReader r = File.OpenText("log.txt"))
        {
            DumpLog (r);
        }
    }
    public static void Log (String logMessage, TextWriter w)
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("  :");
        w.WriteLine("  :{0}", logMessage);
        w.WriteLine ("-------------------------------");
        // Update the underlying file.
        w.Flush();
    }
    public static void DumpLog (StreamReader r)
    {
        // While not at the end of the file, read and write lines.
        String line;
        while ((line=r.ReadLine())!=null)
        {
            Console.WriteLine(line);
        }
        r.Close();
    }
}
 
Share this answer
 
Hi
Try it,



using System.IO;

C#
using (StreamReader sr = new StreamReader(@"c:\teste\chat.txt"))
            {
                string text = string.Format("{0}\n{1}", sr.ReadToEnd(), richTextBox1.Text);
                sr.Close();
                using (StreamWriter sw = new StreamWriter(@"c:\teste\chat.txt"))
                {
                    sw.WriteLine(text);
                }
            }
 
Share this answer
 
It's Quite Simple...

VB
Dim sw As New StreamWriter("C:\aaa.txt")
sw.Write("Anything U Want To Write....")
sw.Close()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900