VB - Reading/Writing from txt file on server

Discussion in 'Mac Programming' started by Aperture, Nov 24, 2007.

  1. macrumors 68000


    Mar 19, 2006
    Hey Everyone,

    I've been learning how to use the IO.StreamReader command in Visual Basic and I wanted to know if it is possible to use IO.StreamReader to read the contents of a txt file on a web server, as opposed to just a local file. Also, is it possible to have VB write to a txt file on a web server?

    The idea behind this is to have an application running on all the client computers and every so often have the application to find a txt file on a webserver and have it display the contents.

  2. Moderator emeritus


    Jun 15, 2000
    I dug up some of my old .NET code and found a C# class I had used. You need to create a WebRequest object, then use its GetResponse() method to get a WebResponse object, then use the WebResponse method GetResponseStream() to return a Stream. From the Stream object you can build a StreamReader to read the downloaded content.
  3. macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    No you can't read files on a server. You have to use HTTP to get anything from a web server.

    But you are in luck because there is a widely used free utility that does what you need "wget" that will pull files from a web server. You don't have to write a program just a short script. Maybe just pipe wget's output to "grep" and look for the string you want.
    See more here http://www.gnu.org/software/wget/

    Put wget inside a short shell script and have it run periodically from the cron table. (See "man cron".)
  4. thread starter macrumors 68000


    Mar 19, 2006
    kainjow, I used your method and I'm still having some trouble. Here is my code so far..

    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Public Class Form1
        Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
            Dim sURL As String
            sURL = "http://www.URL.com/test.txt"
            Dim wrGETURL As WebRequest
            wrGETURL = WebRequest.Create(sURL)
            Dim objStream As Stream
            objStream = wrGETURL.GetResponse.GetResponseStream()
            Dim sr As IO.StreamReader = IO.File.OpenText([COLOR="Red"][U][B]objStream[/B][/U][/COLOR])[I] (See below)
            Dim read As String
            objStream = [COLOR="SeaGreen"][U][B]sr.ReadLine[/B][/U][/COLOR] [I](See below)[/I]
        End Sub
    End Class

    "Value of type 'System.IO.String' cannot be converted to 'String'."

    "Value of type 'String' cannot be converted to 'System.IO.Stream'."

    Any Ideas?

  5. LL2
    macrumors newbie

    Dec 14, 2011
    Reading from .txt file on server.

    Idk about writing a file (I would like to know how to do this also..)
    But for reading a file, you could do this:

    Dim Web As New WebClient
    Dim TXT As String = Web.DownloadString("yourURL.txt")
    BoxWithText.Text = (TXT)

Share This Page