-1

So I'm making a chat program for me and my friend. I am having an issue with writing a message, when I write a message, the entire text file gets replaced with the new message. Here is my code:

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        String write = textBox1.Text;
        try
        {
            StreamWriter sw = new StreamWriter(@"Path.txt");
            sw.WriteLine(write);
            sw.Close();
        }
        catch (Exception w)
        {
            Console.WriteLine("Exception: " + w.Message);
        }
        finally
        {
        }
    }
KlusterMonkey
  • 85
  • 1
  • 8
  • 3
    You're making a *chat program* by writing to a text file? Shouldn't you be doing something more along the lines of TCP/IP? – Ming Slogar Oct 20 '13 at 19:15
  • Use the FileMode enumerator. http://stackoverflow.com/questions/7405828/streamwriter-rewrite-the-file-or-append-to-the-file – Bardicer Oct 20 '13 at 19:15
  • my friend only has dropbox so I'm using a text file in there, its just for me and him. – KlusterMonkey Oct 20 '13 at 19:16

3 Answers3

1

Do not do sw.Close() within the try block! If your I/O code throws an exception, your file remains open (and likely locked) until the application ends.

You can deal with such situations much better by using the using statement.


Code example for appending a string to your file:

using (StreamWriter sw = new StreamWriter(@"Path.txt", true))
{
    sw.WriteLine(write);
}

This is a C# shorthand for:

StreamWriter sw = new StreamWriter(@"Path.txt", true);
try
{
    sw.WriteLine(write);
}
finally
{
    sw.Close();
}

In reality, the way it is implemented, using works with IDisposable objects. (streams implement the IDisposable interface). And using does not call the Close() method in the implicit finally-block, but the Dispose() method. However, streams close the stream (erm...) when the Dispose() method is being called.

In short, whenever you have to control the lifetime of an IDisposable object, use using wherever possible.


But back to topic. Exception handling... You can do the I/O exception handling within the same method, or let one of the calling methods handle the exceptions.

If the method itself should handle exceptions, do it like this:

try
{
    using (StreamWriter sw = new StreamWriter(@"Path.txt", true))
    {
        sw.WriteLine(write);
    }
}
catch (Exception ex)
{
    // Exception handling here...
}

This has the advantage of the file handle already being released before the actual exception is being handled (which, when not done properly, might throw an exception too depending on what you do there).

0

Do you use this method on each text change on a text box? If so, this will be over headed to your application. I suggest you should use this method on enter press or send this chat messages to separate Thread

Izikon
  • 902
  • 11
  • 23
0

A few problems in your ideas:

  1. I see you are doing this in a TextChanged event of a textbox. Every time you enter a letter, or remove one, you are writing the entire content of the textbox to the file. I would recommend only writing a LINE of text to the file when the user presses Enter or some other key.

  2. Creating a new StreamWriter overwrites your file. To append text to the file you could use this:

    using (StreamWriter sw = File.AppendText(path)) 
    {
        sw.WriteLine();
    }
    
Quido
  • 629
  • 1
  • 7
  • 17