看下这个例子:
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Public Class Form2
Dim myThreadDelegate As New ThreadStart(AddressOf filldata)
Protected Delegate Sub Updatedelegate(ByVal progress As Integer)
Public Sub filldata()
Dim request As FtpWebRequest = CType(WebRequest.Create("ftp://localhost/del_test.txt"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
Dim sourceStream As New StreamReader("c:\del_test.txt")
Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
sourceStream.Close()
request.ContentLength = fileContents.Length
Try
Dim flags As Boolean = True
Dim currentposition As Long = 0
Dim len As Integer = 0
Dim requestStream As Stream = request.GetRequestStream()
Do While (flags)
If ((fileContents.Length - currentposition) > 200) Then
len = 200
Else
len = fileContents.Length - currentposition
End If
requestStream.Write(fileContents, currentposition, len)
currentposition = currentposition + len
Me.Invoke(New Updatedelegate(AddressOf updatedisplay), New Object() {CType((currentposition * 100 / fileContents.Length), Integer)})
If (currentposition >= fileContents.Length) Then
flags = False
End If
Loop
requestStream.Close()
Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)
response.Close()
Catch ex As Exception
Console.WriteLine(ex.Message + ex.StackTrace.ToString)
End Try
End Sub
Public Sub updatedisplay(ByVal progress As Integer)
Me.ProgressBar1.Value = progress
If (progress = 100) Then
MessageBox.Show("upload file complete")
End If
Me.ProgressBar1.Invalidate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myThread As New Thread(myThreadDelegate)
myThread.Start()
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Value = 0
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.