How to read from a text file.

Hey guys, back again. I have a text file called "text.txt" which contains a number I want to use as a variable. Im having trouble using File.Open. Can you help with an example, heres a snippet of my code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim path As String = "C: extfile.txt"
Dim rate As FileStream

rate = File.Open(path, FileMode.Open, FileAccess.Read)

Form6.txtRate.Text = rate()
End Sub

Any help appreciated. Thankyou.

Comments

  • : Hey guys, back again. I have a text file called "text.txt" which
    : contains a number I want to use as a variable. Im having trouble
    : using File.Open. Can you help with an example, heres a snippet of my
    : code:
    :
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    : System.EventArgs) Handles MyBase.Load
    :
    : Dim path As String = "C: extfile.txt"
    : Dim rate As FileStream
    :
    : rate = File.Open(path, FileMode.Open, FileAccess.Read)
    :
    : Form6.txtRate.Text = rate()
    : End Sub
    :
    : Any help appreciated. Thankyou.
    :

    [code]
    Dim sr As StreamReader = New StreamReader(rate)
    Dim lNum As Long = CLng(sr.ReadLine())
    [/code]
    Note that this will generate an exception when ReadLine() does not return a valid number. You should first check if it's a number.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : Hey guys, back again. I have a text file called "text.txt" which
    : : contains a number I want to use as a variable. Im having trouble
    : : using File.Open. Can you help with an example, heres a snippet of my
    : : code:
    : :
    : : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    : : System.EventArgs) Handles MyBase.Load
    : :
    : : Dim path As String = "C: extfile.txt"
    : : Dim rate As FileStream
    : :
    : : rate = File.Open(path, FileMode.Open, FileAccess.Read)
    : :
    : : Form6.txtRate.Text = rate()
    : : End Sub
    : :
    : : Any help appreciated. Thankyou.
    : :
    :
    : [code]:
    : Dim sr As StreamReader = New StreamReader(rate)
    : Dim lNum As Long = CLng(sr.ReadLine())
    : [/code]:
    : Note that this will generate an exception when ReadLine() does not
    : return a valid number. You should first check if it's a number.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    Thanks, will try to implement it tomorrow.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories