whosyodaddy
Well-known member
- Joined
- Jun 23, 2003
- Messages
- 83
Ok, I know how to download a update with a progress bar from a past post, although when it downloads a file, It downloads like a .dat or .ini to update your current program. Although how (what do you add) to make it know if it is a newer version or not. So If you click update, and there is no newer version it says "no new version" or something like that. Here is my code:
Code:
Imports System
Imports System.Net
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
This call is required by the Windows Form Designer.
InitializeComponent()
Add any initialization after the InitializeComponent() call
End Sub
Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
Friend WithEvents pbrPercent As System.Windows.Forms.ProgressBar
Friend WithEvents lblInfo As System.Windows.Forms.Label
Friend WithEvents filesizeLabel As System.Windows.Forms.Label
Friend WithEvents Timer As System.Windows.Forms.Timer
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.pbrPercent = New System.Windows.Forms.ProgressBar()
Me.lblInfo = New System.Windows.Forms.Label()
Me.filesizeLabel = New System.Windows.Forms.Label()
Me.Timer = New System.Windows.Forms.Timer(Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
pbrPercent
Me.pbrPercent.Location = New System.Drawing.Point(8, 72)
Me.pbrPercent.Name = "pbrPercent"
Me.pbrPercent.Size = New System.Drawing.Size(320, 20)
Me.pbrPercent.TabIndex = 0
lblInfo
Me.lblInfo.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblInfo.Location = New System.Drawing.Point(16, 16)
Me.lblInfo.Name = "lblInfo"
Me.lblInfo.Size = New System.Drawing.Size(176, 16)
Me.lblInfo.TabIndex = 3
Me.lblInfo.Text = "Demo downloader"
filesizeLabel
Me.filesizeLabel.Font = New System.Drawing.Font("Arial", 6.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.filesizeLabel.ForeColor = System.Drawing.SystemColors.Highlight
Me.filesizeLabel.Location = New System.Drawing.Point(16, 48)
Me.filesizeLabel.Name = "filesizeLabel"
Me.filesizeLabel.Size = New System.Drawing.Size(176, 16)
Me.filesizeLabel.TabIndex = 4
Timer
Me.Timer.Interval = 1000
Button1
Me.Button1.Location = New System.Drawing.Point(200, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(128, 56)
Me.Button1.TabIndex = 6
Me.Button1.Text = "Download!"
Form1
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(338, 103)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.filesizeLabel, Me.lblInfo, Me.pbrPercent})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.TransparencyKey = System.Drawing.Color.Firebrick
Me.ResumeLayout(False)
End Sub
#End Region
Function DownloadFile(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String) As Boolean
Dim wRemote As System.Net.HttpWebRequest
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Dim bBuffer(999) As Byte
Dim iBytesRead As Integer
Try
Reset the progress bar
pProgress.Value = 0
Start the request for the file
URLReq = HttpWebRequest.Create(sURL & Filename)
Get a response from the Request the response will hold header
information from the Request such as filesize
URLRes = URLReq.GetResponse
Open the filestream to write back to, open in create mode so even
if the file exist write over it.
Dim FileStreamer As New FileStream(Application.StartupPath & "\" & Filename, FileMode.Create)
Start getting the data from the response buffer
Dim IncomingData As Stream = URLReq.GetResponse.GetResponseStream
Set the progress bars MAXIMUM to Content length in bytes
pProgress.Maximum = URLRes.ContentLength
Start loop
Loop until there is nothing in the receive buffer
Do
Transfer all the current bytes from the respose buffer
into a byte array and hold the byte count received in
iBytesRead limit to a maximum 1000 bytes
iBytesRead = IncomingData.Read(bBuffer, 0, 1000)
Write all the bytes in the Byte array into the opened
filestream
FileStreamer.Write(bBuffer, 0, iBytesRead)
Code to change the value of the progress bar.
If pProgress.Value + iBytesRead <= pProgress.Maximum Then
pProgress.Value += iBytesRead
Else
pProgress.Value = pProgress.Maximum
End If
Show the current count of KB downloaded KB = BYTES / 1024
filesizeLabel.Text = "Downloading " & CInt(pProgress.Value / 1024) & "kb of " & CInt(URLRes.ContentLength / 1024) & "kb"
Let windows process any events
Application.DoEvents()
Loop if not all bytes have been downloaded
Loop Until iBytesRead = 0
If for some strange reason the progress bar is not at its maximum
value, make it. Can happen with small downloads!
pProgress.Value = pProgress.Maximum
Download done
Close the Response and the File
IncomingData.Close()
FileStreamer.Close()
if successful return true to show this
Return True
Catch
if NOT successful return false to show this and show message
box with the error
MsgBox(Err.Description)
Return False
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DownloadURL As String = "http://www.computerhelp.forum/images/"
If DownloadFile(DownloadURL, pbrPercent, "logo.gif") Then MsgBox("FILE DOWNLOADED!!" & vbCrLf & "Check the \Bin folder to see the download")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class