# Read and write to a text file

**URL:** <https://forums.thehomestack.io/t/read-and-write-to-a-text-file/63>\
**Category:** vb.Net\
**Created:** [October 15, 2003, 3:58am UTC](https://forums.thehomestack.io/t/read-and-write-to-a-text-file/63 "2003-10-15T03:58:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![robinhood1995](https://forums.thehomestack.io/letter_avatar_proxy/v4/letter/r/48db29/32.png) [@robinhood1995](https://forums.thehomestack.io/u/robinhood1995)\
**Post date:** [October 15, 2003, 3:58am UTC](https://forums.thehomestack.io/t/read-and-write-to-a-text-file/63/1 "2003-10-15T03:58:05Z")

</div>

This will read and write to a text file

# Simply creat a windows form and cut an paste the following then add 2 button and 2 text boxes.

Imports [System.IO](http://System.IO)

Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'This Sub Saves to the file

```
    Dim myString As String
    Dim myFile As Integer

    myString = TextBox1.Text
    myFile = FreeFile()

    FileOpen(myFile, "C:\Steve.txt", OpenMode.Output)
    Print(myFile, myString)
    FileClose(myFile)

End Sub

```

Private Sub Button2\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'This Sub read the file

```
    Dim myFile As Integer
    Dim LinesFromFile, NextLine As String

    myFile = FreeFile()

    FileOpen(myFile, "C:\Steve.txt", OpenMode.Input)

    Do Until EOF(myFile)
        NextLine = LineInput(myFile)
        LinesFromFile = LinesFromFile & NextLine & Chr(13) & Chr(10)
    Loop

    TextBox2.Text = LinesFromFile

End Sub

```
