CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    112

    Streamwriter Problem

    Hi, I'm try to write a program that has the user input different values into text boxes and then it puts those values into a text file when the user presses a button. My problem is that I don't want the program to overwrite the text that is already in the text file but add on to the end of it. So if the user presses the button a second time they will add more text to the document. Is there any way to have the streawriter start writing below the last line that has text?

    Thanks,
    Randell

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Streamwriter Problem

    Pass a FileStream object to the StreamWriter and set its FileMode to FileMode.Append. This will create a new file if it doesnt exist, and also append text to the file.

    Code:
    StreamWriter writer = null
    
    try
    {
       using (writer = new StreamWriter(new FileStream("myfile.txt", FileMode.Append)))
       {
          // Write to the stream writer
       }
    }
    catch // Catch exceptions... Disk IO is pesky for exceptions
    {
       // Handle the exception
    }
    finally
    {
       if (writer != null)
       {
          writer.Close();
          writer.Dispose();
       }
    }
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Streamwriter Problem

    So if the user presses the button a second time they will add more
    That's what APPEND does
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured