Solved

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

Posted on 2003-10-27
2
735 Views
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:\vb.net\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



             datdownloader.WriteLine(txtname.Text)
             datdownloader.WriteLine(txtdownloads.Text)
             datdownloader.WriteLine(txtaddress.Text)

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.

bmx

0
Comment
Question by:bmx123
2 Comments
 
LVL 3

Accepted Solution

by:
sony1464 earned 45 total points
Comment Utility
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:\vb.net\files\Downloader.txt") Then
            writer = fso.AppendText("C:\vb.net\files\Downloader.txt")
        Else
            writer = fso.CreateText("C:\vb.net\files\Downloader.txt")
        End If
        writer.WriteLine(txtname.Text)
        writer.WriteLine(txtdownloads.Text)
        writer.WriteLine(txtaddress.Text)
        writer.Close()

End Sub
0
 
LVL 1

Expert Comment

by:Barraza
Comment Utility
btw,
if you do:
        writer = fso.AppendText("C:\vb.net\files\Downloader.txt")

instead of:
        If fso.Exists("C:\vb.net\files\Downloader.txt") Then
            writer = fso.AppendText("C:\vb.net\files\Downloader.txt")
        Else
            writer = fso.CreateText("C:\vb.net\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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now