Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to write to a TextFile ?

Posted on 2006-10-19
4
Medium Priority
?
199 Views
Last Modified: 2010-04-23
Hey whats up guys I would like to know how can I write to a textfile...pretty much any value and as well as
read the value from the textfile incase let say I would like to retrieve the information for other purposes.

I'm currently using VB 2005, and I have never written to a textfile/create/read.
So please if you can give me your insights on this.

Thankyou in advance :)


Remember just keep it simple...I just want to learn to get the hang of inputs/outputs to a file :)
0
Comment
Question by:saruman101
[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
4 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 260 total points
ID: 17768287
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17769123
We could be a little more specific if you gave us info about the format of the file...
0
 
LVL 3

Assisted Solution

by:GeneM
GeneM earned 240 total points
ID: 17770781
Following are 2 subs.  The first writes a file of 10 records.  The second reads those 10 recrods and displays them.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Demonstrates how to create and write to a text file.
        'This test writes 10 text lines to the file
        Dim i As Integer
        Dim writer As StreamWriter = _
            New StreamWriter("c:\TestIt.txt")
        For i = 1 To 10
            writer.WriteLine("This is line " & i.ToString)
        Next i
        writer.Close()
        MsgBox("Write Complete")
        Exit Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles Button2.Click
        'Demonstrates how to read a file by using StreamReader
        'Displays each of the records read
        'try...catch is used to detect a 0 byte file.
        Dim reader As StreamReader = _
            New StreamReader("C:\TestIt.txt")
        Try
            Do 'Until reader.Peek = -1
                MsgBox("Found Record = " & reader.ReadLine)
            Loop Until reader.Peek = -1

        Catch
            MsgBox("Error During Read.  Possibly File is Empty")
        Finally
            reader.Close()
        End Try
    End Sub

I hope these help.
0
 
LVL 3

Expert Comment

by:GeneM
ID: 17770787

One other thing on the streamreader examples above.   Be sure to do a:

Imports System.io

outside the class.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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