Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-27
2
Medium Priority
?
765 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
[X]
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
2 Comments
 
LVL 3

Accepted Solution

by:
sony1464 earned 90 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:\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
ID: 9645020
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

688 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