Solved

How to write to a TextFile ?

Posted on 2006-10-19
4
186 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
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 65 total points
ID: 17768287
0
 
LVL 85

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 60 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

18 Experts available now in Live!

Get 1:1 Help Now