reading and writing to a text file

mcapone888

New member
Joined
Mar 15, 2003
Messages
2
Hi everyone,
I am new to VB.net, and have only played around with VB6 as a hobby. I know this is not the best way to write and read from a file, but this is what I used in the past, and it worked great for me.
I am trying to write some variables to a simple text file, and then be able to use load them again at a later time. In VB 6 I used:

for saving (writing):
Open App.Path & "\file.txt" For Output As #1
Write #1, String1, String2, Integer1
Close #1

for loading (reading):
Open App.Path & "\file.txt" For Input As #1
Input #1, String1, String2, Integer1
Close #1

This wrote the three variables to a simple text file found in the same location as the startup path of the program. How can I recreate this same simple code for a VB.Net windows application? If possible, please let me know the easiest way to do this, even if it is not the correct or best method possible. Thanks!!

Michael
 
Code:
Public Class frmReadWriteTest
    Inherits System.Windows.Forms.Form

    Dim Save1, Save2, Save3 As String
Windows Form Designer generated code


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        
        Save1 = txtOne.Text
        Save2 = txtTwo.Text
        Save3 = txtThree.Text

        FileOpen(1, "test.txt", OpenMode.Output)
        WriteLine(1, Save1, Save2, Save3)
        FileClose(1)
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

        FileOpen(1, "test.txt", OpenMode.Input)
        Input(1, Save1)
        Input(1, Save2)
        Input(1, Save3)
        FileClose(1)

        txtFour.Text = Save1
        txtFive.Text = Save2
        txtSix.Text = Save3
    End Sub


End Class
 
Last edited by a moderator:
Im not sure what your point was behind posting your code. I asked if youd searched the forum yet. There are a number of good examples for doing this the .NET-way posted.
 
arbitrary

I just wrote an arbitrary number of lines to my text file. Now, how can I read them in? I know what each line will contain, but I dont know how many lines. Is there some sort of a peek function to see if there are any more lines available to read?
 
Back
Top