Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

help on downloading a text file from a website into memory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dougsd1r - 12 Mar 2008 17:35 GMT
I have uploaded a text file to my website and can be accessed using a
browser i.e.

http://www.mysite.com/schedule.txt

The text file is just plain text on multiple lines ie

This is a test on line 1
--------------------------------
This is a test on line 3
++++++++++++++++
Line 5

What im trying to achieve is to have a button on my vb form brings up
a msgbox which in turn displays the contexts of the text file
(including the same formatting ie with carriage returns)

I've found sample code on the web which outputs a file to a text box
which i tried to amend  but i found that I lost the carriage returns
or line breaks as these were replaced with spaces.

Can any one help me to change this code or is there another way to go
about it

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load
       Me.TextBox1.Multiline = True
       Me.TextBox1.ScrollBars = ScrollBars.Both
       'above only for showing the sample
       Dim Doc As mshtml.IHTMLDocument2
       Doc = New mshtml.HTMLDocumentClass
       Dim wbReq As Net.HttpWebRequest = _
           DirectCast(Net.WebRequest.Create("http://
msdn.microsoft.com/"), _
           Net.HttpWebRequest)
       Dim wbResp As Net.HttpWebResponse = _
       DirectCast(wbReq.GetResponse(), Net.HttpWebResponse)
       Dim wbHCol As Net.WebHeaderCollection = wbResp.Headers
       Dim myStream As IO.Stream = wbResp.GetResponseStream()
       Dim myreader As New IO.StreamReader(myStream)
       Doc.write(myreader.ReadToEnd())
       Doc.close()
       wbResp.Close()

       'the part below is not completly done for all tags.
       'it can (will) be necessary to tailor that to your needs.

    Dim sb As New System.Text.StringBuilder
       For i As Integer = 0 To Doc.all.length - 1
           Dim hElm As mshtml.IHTMLElement = _
           DirectCast(Doc.all.item(i), mshtml.IHTMLElement)
           Select Case hElm.tagName.ToLower
               Case "body", "html", "head", "form"
               Case Else
                   If hElm.innerText <> "" Then
                       sb.Append(hElm.innerText & vbCrLf)
                   End If
           End Select
       Next
       TextBox1.Text = sb.ToString
   End Sub
End Class

I should add that i am using Visual Basic 2005 Express Edition
kimiraikkonen - 12 Mar 2008 18:14 GMT
> I have uploaded a text file to my website and can be accessed using a
> browser i.e.
[quoted text clipped - 12 lines]
> a msgbox which in turn displays the contexts of the text file
> (including the same formatting ie with carriage returns)

Why don't you just use webbrowser control and place / dock this
control on a extra form, then navigate to text file using
webbrowser1.navigate("http://www.mysite.com/schedule.txt"). Then call
this extra form from your main vb form using show method? As i
understand from your goal.
Dougsd1r - 12 Mar 2008 18:57 GMT
thanks i will do that
didnt think of doing it that way,
i am trying to convert a progam i wrote in vb6 which did it they way i
mentioned above but your way is a lot simpler
thanks again
Herfried K. Wagner [MVP] - 12 Mar 2008 21:41 GMT
"Dougsd1r" <dougsdir@gmail.com> schrieb:
>I have uploaded a text file to my website and can be accessed using a
> browser i.e.
[quoted text clipped - 12 lines]
> a msgbox which in turn displays the contexts of the text file
> (including the same formatting ie with carriage returns)

Take a look at 'WebClient.DownloadString'.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

ShaneO - 12 Mar 2008 21:52 GMT
> What im trying to achieve is to have a button on my vb form brings up
> a msgbox which in turn displays the contexts of the text file
> (including the same formatting ie with carriage returns)

You can do it with the following code -

Dim WebClient As New System.Net.WebClient
Try
  MsgBox(WebClient.DownloadString("http://www.mysite.com/schedule.txt")
Catch ex As Exception
  'Error handling in here
End Try

At the time of this post, your site is currently not available, so you
will receive the following Exception -

"The remote server returned an error: (404) Not Found."

To me, this is the simplest way to achieve what you require and has the
added advantage of being able to easily handle errors etc.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Just_a_fan@home.net - 15 Mar 2008 00:43 GMT
From reading books, it seems you need to use a Rich Text Box rather than
the regular one.

Mike

On Wed, 12 Mar 2008 09:35:52 -0700 (PDT), in
microsoft.public.dotnet.languages.vb Dougsd1r <dougsdir@gmail.com>
wrote:

>I have uploaded a text file to my website and can be accessed using a
>browser i.e.
[quoted text clipped - 61 lines]
>
>I should add that i am using Visual Basic 2005 Express Edition

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.