?
Solved

How to append to a text file in VB.Net

Posted on 2005-04-28
2
Medium Priority
?
1,998 Views
Last Modified: 2012-05-05
I am trying to write a simple vb program that takes input from a form and writes to a text file.  I have tried to work it out using xml and I have not had much luck, so I have decided to go back to writing to a text file.  Here is what I have so far, my problem is that the program will not append to the file. It just keeps overwriting the first line.  If their is a better way to code this, could someone please explain.

Thanks

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        Dim oFile As System.IO.File
        Dim objWriter As System.IO.StreamWriter
        objWriter = oFile.CreateText("\my documents\data.txt")
        Dim stop_time As Date = Now
   
        Dim FILE_NAME As String = "\my documents\data.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then



            objWriter.Write((Eq_Num.Text) + ",")
            objWriter.Write((ComboBox1.Text) + ",")
            objWriter.Write((ComboBox2.Text) + ",")
            objWriter.Write((ComboBox3.Text) + ",")
            objWriter.Write((ComboBox4.Text) + ",")
            objWriter.Write((ComboBox5.Text) + ",")
            objWriter.Write((ComboBox6.Text) + ",")
            objWriter.WriteLine((stop_time) + ",")
            objWriter.Flush()
            objWriter.Close()
            MsgBox("Text written to file")

        Else
            MsgBox("File Does Not Exist")
        End If

       
    End Sub
0
Comment
Question by:maximjon
2 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 1000 total points
ID: 13889837
instead of oFile.CreateTEext(...) try using:

oFile.AppendText("\my documents\data.txt")
0
 
LVL 10

Expert Comment

by:heintalus
ID: 13889907
Try changing it to the following that way it will also create the file if it doesn't exist

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

        Dim objWriter As System.IO.StreamWriter
        Dim stop_time As Date = Now
        Dim FILE_NAME As String = "\my documents\data.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then
            objWriter = System.IO.File.AppendText(FILE_NAME)
        Else
            objWriter = System.IO.File.CreateText(FILE_NAME)
        End If

        objWriter.Write((Eq_Num.Text) + ",")
        objWriter.Write((ComboBox1.Text) + ",")
        objWriter.Write((ComboBox2.Text) + ",")
        objWriter.Write((ComboBox3.Text) + ",")
        objWriter.Write((ComboBox4.Text) + ",")
        objWriter.Write((ComboBox5.Text) + ",")
        objWriter.Write((ComboBox6.Text) + ",")
        objWriter.WriteLine((stop_time) + ",")
        objWriter.Flush()
        objWriter.Close()

        MsgBox("Text written to file")
       
End Sub

HTH
Andy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question