VB - Reading/Writing from txt file on server

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

  1. Aperture
    Expand Collapse
    macrumors 68000

    Aperture

    Joined:
    Mar 19, 2006
    Location:
    PA
    #1
    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.

    Thanks
     
  2. kainjow
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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. ChrisA
    Expand Collapse
    macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3
    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. Aperture
    Expand Collapse
    thread starter macrumors 68000

    Aperture

    Joined:
    Mar 19, 2006
    Location:
    PA
    #4
    kainjow, I used your method and I'm still having some trouble. Here is my code so far..

    Code:
    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)
    [/I]
            Dim read As String
            objStream = [COLOR="SeaGreen"][U][B]sr.ReadLine[/B][/U][/COLOR] [I](See below)[/I]
    
            lstOutput.Items.Add(read)
    
            objStream.Close()
        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?

    Thanks
     
  5. LL2
    Expand Collapse
    macrumors newbie

    Joined:
    Dec 14, 2011
    #5
    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:

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

Share This Page