MNJ(m-studio)

Hi Guys!! Smile

back again.. Wink

I want to make my program capable of checking their own versions against a file on my server.!
but obviusly my.computer.filesystem.readalltext() aint doing the trick!

is there a way to make it read the content of a remote file
and perhaps to downlaod it (: so i can make an updater for my programs


hope you can help!

//Martin Jorgensen



Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

hmm nobody knows this ):





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Martin Xie - MSFT

Hi MNJ,

Here are the two solutions to downloading file and replacing old file (e.g. D:\Text.txt).

1. Download file from remote server using FileWebRequest and FileWebResponse classes.

Prerequisites: Button1, Label1 and ProgressBar1 on Form1.

Code Block

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim file As String = "\\martin\Share\file.txt"

Dim request As System.Net.WebRequest = System.Net.FileWebRequest.Create(file)

Dim response As System.Net.FileWebResponse = request.GetResponse()

Dim stream As System.IO.Stream = response.GetResponseStream()

' Get the length of the content

Dim length As Integer = response.ContentLength

' Set the maximum length of the progress bar.

ProgressBar1.Maximum = length

' Create a temporary array for the content of the file.

Dim bytes(length) As Byte

' Get all bytes of the content and advance the progress bar.

For i As Integer = 0 To length - 1

bytes(i) = stream.ReadByte()

ProgressBar1.Value = i

Label1.Text = i.ToString + "Bytes Downloaded"

Application.DoEvents()

Next

' Write the content to the output file.

Using output As IO.Stream = System.IO.File.Create("D:\Text.txt")

output.Write(bytes, 0, bytes.Length)

End Using

End Sub

2. Download file from internet/website using HttpWebRequest and HttpWebResponse classes.

Prerequisites: Button1, Label1 and ProgressBar1 on Form1.

Code Block

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim file As String = "http://URL/file.txt"

Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(file)

Dim response As System.Net.HttpWebResponse = request.GetResponse()

Dim stream As System.IO.Stream = response.GetResponseStream()

' Get the length of the content

Dim length As Integer = response.ContentLength

' Set the maximum length of the progress bar.

ProgressBar1.Maximum = length

' Create a temporary array for the content of the file.

Dim bytes(length) As Byte

' Get all bytes of the content and advance the progress bar.

For i As Integer = 0 To length - 1

bytes(i) = stream.ReadByte()

ProgressBar1.Value = i

Label1.Text = i.ToString + "Bytes Downloaded"

Application.DoEvents()

Next

' Write the content to the output file.

Using output As IO.Stream = System.IO.File.Create("D:\Text.txt")

output.Write(bytes, 0, bytes.Length)

End Using

End Sub

I hope that can help you.

Regards,

Martin






Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Martin Xie - MSFT

MNJ(m-studio) wrote:

but obviusly my.computer.filesystem.readalltext() aint doing the trick!
is there a way to make it read the content of a remote file

Read the content of a remote Text file into RichTextBox control.

Code Block

Imports System.IO

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim fs As StreamReader = New StreamReader("\\Martin\Share\grid.txt")

RichTextBox1.Text = fs.ReadToEnd()

End Sub






Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Martin Xie - MSFT

Please check this thread about how to compare two files.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2122111&SiteID=1

Thanks,

Martin






Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

Hey..! thanks for the help! Smile





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

Hi..!

it works.. but only VERY slow.. ):

it only uses like 1% of the available internet (downlaod rate)...
how can i speed it up Surprise if there is a way.. Tongue Tied





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Martin Xie - MSFT

Hi MNJ,

The download speed is very slow...

Suggest you to create a new thread at Networking and Communication forum for better responses to How to improve download speed. Please remember to post your existing code.

.NET Development .NET Framework Networking and Communication

Thanks,

Martin






Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Rafael Altungy

Hello Martin:

Use the function My.Computer.Network.DownloadFile(), it downloads a file from web.

Regards

Rafael Altungy

SPAIN





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Martin Xie - MSFT

Oh! Thank you Rafael for your nice solution!

I try using it to download file from remote server and internet/website, it works fine.

The download speed is very fast.

My.Computer.Network.DownloadFile("http://download.sysinternals.com/Files/Handle.zip", "D:\1.zip")

My.Computer.Network.DownloadFile("\\martin\Share\Handle.zip", "D:\2.zip")

My.Computer.Network.DownloadFile Method

Downloads the specified remote file and saves it in the specified location.

Thanks,

Martin






Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

well i have already tried that.. the issue is just, how do i get the progress bar to update
and why doesn't it always downlaod the whole file but jsut stops after x seconds





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Rafael Altungy

Try changing the sixth parameter (connectionTimeout) that control the waiting time in miliseconds. The default value is 100,000. I have experienced the same problem depending day or the hour, probably because the DSL condition.

If you want to see the progress of the download yo must put the fifth parameter (showUI) to True (the default is False).

Regards

Rafael Altungy

SPAIN





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

thanks,, ill try that Smile

but when i have show ui to true.. how to i put the progress in my progress bar Smile





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

MNJ(m-studio)

uhhhh.. it kindof helps... but,,, it's not supposed to stop after the first byte.. Tongue Tied

why does it stall i set timeout to 10000000 and it jsut stops..
and what do i put in username/password it keeps saying i need theese arguments





Re: Visual Basic Express Edition Reading content of online files? comparing with content in remote files (help needed)

Rafael Altungy

Hello:

To the first question I don't know.

To the secon put as the parameter "". You must put this because you need the following parameters.

Regards

Rafael Altungy

SPAIN