PDA

View Full Version : VB - Reading/Writing from txt file on server




Aperture
Nov 24, 2007, 08:57 PM
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



kainjow
Nov 25, 2007, 01:00 PM
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.

ChrisA
Nov 25, 2007, 02:10 PM
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

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".)

Aperture
Nov 29, 2007, 09:48 PM
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(objStream) (See below)

Dim read As String
objStream = sr.ReadLine (See below)

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

LL2
Dec 14, 2011, 01:30 PM
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)