📄 downloadfile.vb
字号:
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.WinForms
Imports System.Data
Imports System.Net
Imports System.Threading
Imports System.IO
Imports System.Text
Public Class frmFileDownload
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private WithEvents OpenFileDialog1 As System.WinForms.OpenFileDialog
Private WithEvents txtProxyServer As System.WinForms.TextBox
Private WithEvents txtPort As System.WinForms.TextBox
Private WithEvents txtProxyUserName As System.WinForms.TextBox
Private WithEvents txtProxyPassword As System.WinForms.TextBox
Private WithEvents label5 As System.WinForms.Label
Private WithEvents label6 As System.WinForms.Label
Private WithEvents label7 As System.WinForms.Label
Private WithEvents label8 As System.WinForms.Label
Private WithEvents checkBox1 As System.WinForms.CheckBox
Private WithEvents groupBox1 As System.WinForms.GroupBox
Private WithEvents cmdDirectory As System.WinForms.Button
Private WithEvents cmdDownload As System.WinForms.Button
Private WithEvents txtFileName As System.WinForms.TextBox
Private WithEvents lblStatus As System.WinForms.Label
Private WithEvents prgFileDownload As System.WinForms.ProgressBar
Private WithEvents cmdCancel As System.WinForms.Button
Private WithEvents label4 As System.WinForms.Label
Private WithEvents txtURL As System.WinForms.TextBox
Private WithEvents txtUserName As System.WinForms.TextBox
Private WithEvents txtPassword As System.WinForms.TextBox
Private WithEvents label1 As System.WinForms.Label
Private WithEvents label2 As System.WinForms.Label
Private WithEvents label3 As System.WinForms.Label
Private WithEvents groupBox2 As System.WinForms.GroupBox
Dim WithEvents Form1 As System.WinForms.Form
'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.
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.label8 = New System.WinForms.Label()
Me.txtProxyPassword = New System.WinForms.TextBox()
Me.txtURL = New System.WinForms.TextBox()
Me.label5 = New System.WinForms.Label()
Me.label6 = New System.WinForms.Label()
Me.txtPassword = New System.WinForms.TextBox()
Me.groupBox1 = New System.WinForms.GroupBox()
Me.txtPort = New System.WinForms.TextBox()
Me.cmdDirectory = New System.WinForms.Button()
Me.OpenFileDialog1 = New System.WinForms.OpenFileDialog()
Me.groupBox2 = New System.WinForms.GroupBox()
Me.cmdDownload = New System.WinForms.Button()
Me.lblStatus = New System.WinForms.Label()
Me.txtUserName = New System.WinForms.TextBox()
Me.checkBox1 = New System.WinForms.CheckBox()
Me.prgFileDownload = New System.WinForms.ProgressBar()
Me.txtProxyUserName = New System.WinForms.TextBox()
Me.txtProxyServer = New System.WinForms.TextBox()
Me.label7 = New System.WinForms.Label()
Me.label4 = New System.WinForms.Label()
Me.txtFileName = New System.WinForms.TextBox()
Me.cmdCancel = New System.WinForms.Button()
Me.label1 = New System.WinForms.Label()
Me.label2 = New System.WinForms.Label()
Me.label3 = New System.WinForms.Label()
'@design Me.TrayHeight = 90
'@design Me.TrayLargeIcon = False
'@design Me.TrayAutoArrange = True
label8.Location = New System.Drawing.Point(72, 88)
label8.Text = "Password"
label8.Size = New System.Drawing.Size(80, 16)
label8.TabIndex = 1
txtProxyPassword.Location = New System.Drawing.Point(160, 88)
txtProxyPassword.PasswordChar = CChar(42)
txtProxyPassword.TabIndex = 5
txtProxyPassword.Size = New System.Drawing.Size(136, 20)
txtURL.Location = New System.Drawing.Point(80, 16)
txtURL.TabIndex = 5
txtURL.Size = New System.Drawing.Size(216, 20)
label5.Location = New System.Drawing.Point(16, 24)
label5.Text = "Server"
label5.Size = New System.Drawing.Size(56, 16)
label5.TabIndex = 4
label6.Location = New System.Drawing.Point(208, 24)
label6.Text = "Port"
label6.Size = New System.Drawing.Size(24, 16)
label6.TabIndex = 3
txtPassword.Location = New System.Drawing.Point(160, 64)
txtPassword.PasswordChar = CChar(42)
txtPassword.TabIndex = 3
txtPassword.Size = New System.Drawing.Size(136, 20)
groupBox1.Location = New System.Drawing.Point(8, 112)
groupBox1.TabIndex = 8
groupBox1.TabStop = False
groupBox1.Size = New System.Drawing.Size(304, 120)
txtPort.Location = New System.Drawing.Point(240, 24)
txtPort.TabIndex = 7
txtPort.Size = New System.Drawing.Size(56, 20)
cmdDirectory.Location = New System.Drawing.Point(280, 240)
cmdDirectory.Size = New System.Drawing.Size(32, 24)
cmdDirectory.TabIndex = 7
cmdDirectory.Text = "[...]"
'@design OpenFileDialog1.SetLocation(New System.Drawing.Point(7, 7))
groupBox2.Location = New System.Drawing.Point(8, 8)
groupBox2.TabIndex = 0
groupBox2.TabStop = False
groupBox2.Size = New System.Drawing.Size(304, 96)
cmdDownload.Location = New System.Drawing.Point(112, 320)
cmdDownload.Size = New System.Drawing.Size(88, 24)
cmdDownload.TabIndex = 6
cmdDownload.Text = "Download !"
lblStatus.Location = New System.Drawing.Point(8, 296)
lblStatus.Size = New System.Drawing.Size(304, 16)
lblStatus.TabIndex = 4
txtUserName.Location = New System.Drawing.Point(160, 40)
txtUserName.TabIndex = 4
txtUserName.Size = New System.Drawing.Size(136, 20)
checkBox1.Location = New System.Drawing.Point(16, 0)
checkBox1.Text = "Proxy Server"
checkBox1.Size = New System.Drawing.Size(88, 16)
checkBox1.TabIndex = 0
prgFileDownload.Location = New System.Drawing.Point(8, 272)
prgFileDownload.TabIndex = 3
prgFileDownload.Size = New System.Drawing.Size(304, 16)
txtProxyUserName.Location = New System.Drawing.Point(160, 56)
txtProxyUserName.TabIndex = 6
txtProxyUserName.Size = New System.Drawing.Size(136, 20)
txtProxyServer.Location = New System.Drawing.Point(80, 24)
txtProxyServer.TabIndex = 8
txtProxyServer.Size = New System.Drawing.Size(120, 20)
label7.Location = New System.Drawing.Point(72, 56)
label7.Text = "User Name"
label7.Size = New System.Drawing.Size(80, 16)
label7.TabIndex = 2
label4.Location = New System.Drawing.Point(24, 240)
label4.Text = "File Directory"
label4.Size = New System.Drawing.Size(80, 16)
label4.TabIndex = 1
txtFileName.Location = New System.Drawing.Point(96, 240)
txtFileName.TabIndex = 5
txtFileName.Size = New System.Drawing.Size(176, 20)
cmdCancel.Location = New System.Drawing.Point(224, 320)
cmdCancel.Size = New System.Drawing.Size(88, 24)
cmdCancel.TabIndex = 2
cmdCancel.Text = "Cancel"
label1.Location = New System.Drawing.Point(16, 16)
label1.Text = "URL"
label1.Size = New System.Drawing.Size(48, 16)
label1.TabIndex = 2
label2.Location = New System.Drawing.Point(72, 48)
label2.Text = "User Name"
label2.Size = New System.Drawing.Size(80, 16)
label2.TabIndex = 1
label3.Location = New System.Drawing.Point(72, 64)
label3.Text = "Password"
label3.Size = New System.Drawing.Size(80, 16)
label3.TabIndex = 0
Me.Text = "HTTP file Downloader"
Me.MaximizeBox = False
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BorderStyle = System.WinForms.FormBorderStyle.FixedDialog
Me.ClientSize = New System.Drawing.Size(322, 351)
Me.Controls.Add(groupBox1)
Me.Controls.Add(cmdDirectory)
Me.Controls.Add(cmdDownload)
Me.Controls.Add(txtFileName)
Me.Controls.Add(lblStatus)
Me.Controls.Add(prgFileDownload)
Me.Controls.Add(cmdCancel)
Me.Controls.Add(label4)
Me.Controls.Add(groupBox2)
groupBox1.Controls.Add(checkBox1)
groupBox1.Controls.Add(label8)
groupBox1.Controls.Add(label7)
groupBox1.Controls.Add(label6)
groupBox1.Controls.Add(label5)
groupBox1.Controls.Add(txtProxyPassword)
groupBox1.Controls.Add(txtProxyUserName)
groupBox1.Controls.Add(txtPort)
groupBox1.Controls.Add(txtProxyServer)
groupBox2.Controls.Add(label3)
groupBox2.Controls.Add(label2)
groupBox2.Controls.Add(label1)
groupBox2.Controls.Add(txtPassword)
groupBox2.Controls.Add(txtUserName)
groupBox2.Controls.Add(txtURL)
End Sub
#End Region
Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Application.Exit()
End Sub
Protected Sub checkBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If (checkBox1.Checked) Then
EnableProxy()
Else
DisableProxy()
End If
End Sub
Protected Sub cmdDirectory_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iResult As DialogResult
With openFileDialog1
.CheckFileExists = True
.CheckPathExists = True
.InitialDirectory = "C:\"
iResult = openFileDialog1.ShowDialog()
Select Case (iResult)
Case DialogResult.OK
Dim s As String = openFileDialog1.FileName.ToString()
Dim i As Integer = s.LastIndexOf("\")
If (i > 0) Then
txtFileName.Text = s.Substring(0, i)
End If
Case Else
txtFileName.Text = "C:\"
End Select
End With
End Sub
Protected Sub cmdDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Me.cmdDownload.Enabled = False
Dim url As String = txtURL.Text.ToString()
If url = "" Then
Throw New ApplicationException("URL must be provided..")
End If
Dim directory As String = txtFileName.Text.ToString()
Dim normalUser As String = txtUserName.Text.ToString()
Dim normalPass As String = txtPassword.Text.ToString()
Dim proxyServer As String = txtProxyServer.Text.ToString()
Dim proxy As Boolean = checkBox1.Checked.ToBoolean()
Dim proxyUser As String = Nothing
Dim proxyPass As String = Nothing
Dim proxyPort As Integer = 80
If (proxy) Then
proxyUser = txtProxyUserName.Text.ToString()
proxyPass = txtProxyPassword.Text.ToString()
If (txtPort.Text = "") Then
txtPort.Text = "80"
End If
proxyPort = txtPort.Text.ToInt32()
Else
End If
'CDownLoad download=new CDownLoad("http://www.mindcracker.com/csharp/1/COMinDOTNET.zip","c:\\temp",this)
Dim download As CDownload = New CDownLoad(url, normalUser, normalPass, proxy, proxyServer, proxyPort, proxyUser, proxyPass, directory, Me)
Dim t As Thread = New Thread(New ThreadStart(AddressOf download.downloadFile))
t.Start()
Catch ex As Exception
MessageBox.Show("Some Error occured while downloading file try again" & ex.tostring())
Me.cmdDownload.Enabled = True
End Try
End Sub
Private Sub DisableProxy()
txtPort.Enabled = False
txtProxyPassword.Enabled = False
txtProxyUserName.Enabled = False
txtProxyServer.Enabled = False
End Sub
Private Sub EnableProxy()
txtPort.Enabled = True
txtPort.Text = "80"
txtProxyPassword.Enabled = True
txtProxyUserName.Enabled = True
txtProxyServer.Enabled = True
End Sub
Public Sub HandleStatus(ByVal FileSize As Integer, ByVal started As Boolean)
Me.lblStatus.Text = "File Size Remaining : " & FileSize
If (Not started) Then
Try
prgFileDownload.Maximum = FileSize
prgFileDownload.Value = FileSize
Catch Ex as Exception
End Try
Else
If (FileSize >= 0) Then
prgFileDownload.Value = FileSize
End If
If (FileSize = 0) Then
Me.cmdDownload.Enabled = True
End If
End If
End Sub
Public Sub EnableDownloadAgainAfterError(ByVal e As Exception)
MessageBox.Show(e.ToString(), "Error in HTTP download", MessageBox.OK)
cmdDownload.Enabled = True
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -