How to create and add data to a text file in VB.NET

Posted on 2003-10-27
Last Modified: 2007-12-19
I have three text boxes and an Add button. I want basically to create a Downloader.txt file in the directory c:\\files\Downloader.txt
I'm trying to use the following code but unable to create the above text file and add the data from three textboxes to the Downloader.txt file.

Primarily I want to check the Downloader.txt file exists in the above directory, if not I want to create it or if it exists I want to append the data to the file.

Here is the code:

 Dim datdownloader As New StreamWriter("downloader.txt", True)

             Dim strrecord As String

             If IsNumeric(txtname.Text) Then
                 MessageBox.Show("Enter A string Value ", "Invalid data Entry", _
                 MessageBoxButtons.OK, MessageBoxIcon.Warning)

             End If


strrecord = (txtname.Text & "|" & txtdownloads.Text & "|" & txtaddress.Text)

'I WANT TO ADD THE ABOVE strrecord to the Downloader.txt file if it exits, if not i want to create and append the strrecord to that file.

I appreciate your response.


Question by:bmx123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

sony1464 earned 45 total points
ID: 9631088
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        Dim fso As File
        Dim writer As StreamWriter
        If fso.Exists("C:\\files\Downloader.txt") Then
            writer = fso.AppendText("C:\\files\Downloader.txt")
            writer = fso.CreateText("C:\\files\Downloader.txt")
        End If

End Sub

Expert Comment

ID: 9645020
if you do:
        writer = fso.AppendText("C:\\files\Downloader.txt")

instead of:
        If fso.Exists("C:\\files\Downloader.txt") Then
            writer = fso.AppendText("C:\\files\Downloader.txt")
            writer = fso.CreateText("C:\\files\Downloader.txt")
        End If

it will get the same result.  If the file does not exist, it will create it and add to it.  If it does exist, it will add to it.

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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