Solved

How to write to a TextFile ?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 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