Solved

How to write to a TextFile ?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

929 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

14 Experts available now in Live!

Get 1:1 Help Now