borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (HELP, I\'M DESPERATE!)Next Topic (MSAgent in vb.net) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Reading a File
Poster Message
c9679
Level: Guest


icon Reading a File

Hello,

Does anyone know how to open a text file in VB .net from a URL please

eg if i have a file http://www.mysite.com/header.txt does anyone know how I could just read that and display the contents please

23-06-2003 at 02:58 PM
| Quote Reply
zimcoder
Level: VB Lord


Registered: 27-10-2003
Posts: 225
icon Re: Reading a File

This may not fully address your problem but i trust it will point you in the right direction.

to do file input and output in vb.net you need to import the necessary namespace :

Imports System.IO

you will need to create an instance of the StreamReader class
one of the StreamReader constructors takes the file path as its argument :
Dim sr as StreamReader
sr = New StreamReader("\\path")
you must put this in a try catch block to catch all the likely errors and exceptions such as FileNotFoundException ..etc

here are the essential sections of the sample code that i have written:

'include the namspace
Imports System.IO

'declare the StreamReader Instance
Dim sr as StreamReader

'the variables
Friend WithEvents txtFilePath As System.Windows.Forms.TextBox
    Friend WithEvents lblFilePath As System.Windows.Forms.Label
    Friend WithEvents txtReadText As System.Windows.Forms.TextBox
    Friend WithEvents btnReadFile As System.Windows.Forms.Button

'the sub that does all the work

Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
        If txtFilePath.Text = Nothing Then
            MessageBox.Show("Please enter a file path name", "Warning")
        Else
            Try
                sr = New StreamReader(txtFilePath.Text)
                Dim strtext As String = sr.ReadToEnd()
                txtReadText.Text = strtext
                sr.Close()
            Catch ex As FileNotFoundException
                MessageBox.Show("File was Not Found", "Warning")
            End Try
        End If
    End Sub

  if you are dealing with TCP/IP streams then you will have to use the stream class that comes within the same nasmepace, System.IO. this allows you to read streams of data as bytes across the network. hope this will start you off in the right direction

____________________________
BrainBench ADO.NET and ASP.NET Certified Developer

20-11-2003 at 08:36 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Reading a File
Previous Topic (HELP, I\'M DESPERATE!)Next Topic (MSAgent in vb.net) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder