• 0

[VB.NET] Display Contents Of Txt File


Question

Hey everyone,

I am working on a radio player for a interent radio station and want to display the contents of a text file that is stored on the internet, does anyone know how i can do this.

Cheers

Richard

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

You need to create a WebRequest instance to make the call to the remote server to get the text file:

' The url of the file.
Dim url as String = "http://www.mydomain.com/file.txt"

' Create our web request, and get its response.
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response As HttpWebResponse = request.GetResponse()

' Read the content from the stream.
Dim reader As StreamReader = new StreamReader(response.GetResponseStream())
Dim content As String = reader.ReadToEnd()

' Dispose of our managed types.
reader.Dispose()
response.Dispose()
request.Dispose()

Link to comment
Share on other sites

  • 0

Wrap it up inside a function:

Import System.IO
Import System.Web

Public Function GetRemoteFile(ByVal url As String) As String
  ' Create our web request, and get its response.
  Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
  Dim response As HttpWebResponse = request.GetResponse()

  ' Read the content from the stream.
  Dim reader As StreamReader = new StreamReader(response.GetResponseStream())
  Dim content As String = reader.ReadToEnd()

  ' Dispose of our managed types.
  reader.Dispose()
  response.Dispose()
  request.Dispose()

  Return content
End Function

And then you can call it as such:

Dim remoteFileContent As String = GetRemoteFile("http://www.mydomain.com/test.txt")

Just add it in wherever you feel is appropriate.

Link to comment
Share on other sites

  • 0

Imports Un4seen.Bass
Imports System.IO
Imports System.Web
Imports System.Net

    Public Function GetRemoteFile(ByVal url As String) As String

        ' The url of the file.
        Dim url As String = "http://dl.dropbox.com/u/1038051/test.txt"

        ' Create our web request, and get its response.
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = request.GetResponse()

        ' Read the content from the stream.
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim content As String = reader.ReadToEnd()

        ' Dispose of our managed types.
        'reader.Dispose()
        'response.Dispose()
        'request.Dispose()
        Return content
    End Function

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt")
    End Sub
End Class

Link to comment
Share on other sites

  • 0

Ah, in my refactored function, I removed the url declaration as its declared earlier as a parameter:

Public Function GetRemoteFile(ByVal url As String) As String
  ' Create our web request, and get its response.
  Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
  Dim response As HttpWebResponse = request.GetResponse()

  ' Read the content from the stream.
  Dim reader As StreamReader = new StreamReader(response.GetResponseStream())
  Dim content As String = reader.ReadToEnd()

  ' Dispose of our managed types.
  reader.Dispose()
  response.Dispose()
  request.Dispose()

  Return content
End Function

Link to comment
Share on other sites

  • 0

Ah, my bad, HttpWebRequest and HttpWebResponse Dispose methods are protected. You can remove them from your code, so remove the lines:

request.Dispose()
response.Dispose()

Link to comment
Share on other sites

  • 0

Yes, the app runs now but if i call the function inside a label function the text dosent display that is inside the .txt file and i know their is some in their http://dl.dropbox.com/u/1038051/test.txt

 Public Function GetRemoteFile(ByVal url As String) As String
        ' Create our web request, and get its response.
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = request.GetResponse()

        ' Read the content from the stream.
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim content As String = reader.ReadToEnd()

        ' Dispose of our managed types.
        reader.Dispose()

        Return content
    End Function

    Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt")
    End Sub
End Class

Link to comment
Share on other sites

  • 0

This:

    Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt")
    End Sub

Doesn't do anything useful. It just assigns the contents of the file to a variable. There's no logic in there to display it in any fashion.

Link to comment
Share on other sites

  • 0

This:

    Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt")
    End Sub

Doesn't do anything useful. It just assigns the contents of the file to a variable. There's no logic in there to display it in any fashion.

Do you have any code that may help me display it? :rolleyes:

Link to comment
Share on other sites

  • 0

Lol, add a textbox to your form, and then set the content:

TextBox1.Text = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt")

We can only show you the door, its up to you to explore what's on the other side.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.