我试图自己做这件事,但遇到了需要帮助的情况。我想使用进度条控件来显示 FTP 文件上传的当前进度。
目前,我正在手动更改进度条控件的值 - 但我不禁认为那里可能有更好或更简单的方法。它现在可以工作,但是进度条在显示基于正在执行的代码部分的进度时是零星的。此外,我试图将整个子程序放入一个单独的线程中,但注意到当我这样做时,进度条直到代码结束才会显示 - 然后它会短暂闪烁并再次隐藏。
这是我到目前为止所做的,任何帮助将不胜感激:
Public Sub uploadAuthorization()
ProgressBar1.Show()
Dim fileName As String = Path.GetFileName(TextBoxFilePath.Text)
Dim ftpFolder As String = "authorizations"
Try
'Create FTP Request
Me.Cursor = Cursors.WaitCursor
Dim myRequest As FtpWebRequest = DirectCast(WebRequest.Create(ftpServer + "/" + ftpFolder + "/" + fileName), FtpWebRequest)
ProgressBar1.Value = 20
'Update properties
myRequest.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
myRequest.Method = WebRequestMethods.Ftp.UploadFile
ProgressBar1.Value = ProgressBar1.Value + 20
'Read the file
Dim myFile As Byte() = File.ReadAllBytes(TextBoxFilePath.Text)
ProgressBar1.Value = ProgressBar1.Value + 20
'Upload the file
Dim myStream As Stream = myRequest.GetRequestStream()
myStream.Write(myFile, 0, myFile.Length)
ProgressBar1.Value = ProgressBar1.Value + 20
'Cleanup
myStream.Close()
myStream.Dispose()
ProgressBar1.Value = ProgressBar1.Value + 20
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
Me.Cursor = Cursors.Arrow
End Sub