Solved

How to write to a TextFile ?

Posted on 2006-10-19
4
196 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 65 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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