Hallo,
ich habe mich heute zum ersten Mal an VB.net rangetraut. Komme normalerweise eher aus den Scriptsprachen wie Javascript und PHP und tu mich noch etwas schwer.
Einige kleine Anwendungen habe ich Autoit umgesetzt, was aber schnell an seine Grenzen stößt. Nun versuche ich einige Projekte auf VB.NET umzuziehen.
Ich schreibe einen Downloader, der anhand von zwei eingegeben Kriterien eine INI-Datei auf dem FTP ausliest, die widerum die echten Downloadinfos enthält.
Funktioniert wie ne 1
Sobald ich den Download-Button drücke, ändert der seinen Text in Cancel
Der Download klappt und auch die Progressbar wird richtig geupdated.
Nun ist die Anwendung aber so krass mit dem Download beschäftigt, dass ich den "neuen" Cancel button nicht mehr clicken kann.
Der Download läuft in einem Loop.
Ich weiß, ich hätte viel mehr in Funktionen auslagern können, aber das kommt danach, wenn endlich mal alles steht
Hier der Code des Downloadbuttons:
ich habe mich heute zum ersten Mal an VB.net rangetraut. Komme normalerweise eher aus den Scriptsprachen wie Javascript und PHP und tu mich noch etwas schwer.
Einige kleine Anwendungen habe ich Autoit umgesetzt, was aber schnell an seine Grenzen stößt. Nun versuche ich einige Projekte auf VB.NET umzuziehen.
Ich schreibe einen Downloader, der anhand von zwei eingegeben Kriterien eine INI-Datei auf dem FTP ausliest, die widerum die echten Downloadinfos enthält.
Funktioniert wie ne 1
Sobald ich den Download-Button drücke, ändert der seinen Text in Cancel
Der Download klappt und auch die Progressbar wird richtig geupdated.
Nun ist die Anwendung aber so krass mit dem Download beschäftigt, dass ich den "neuen" Cancel button nicht mehr clicken kann.
Der Download läuft in einem Loop.
Ich weiß, ich hätte viel mehr in Funktionen auslagern können, aber das kommt danach, wenn endlich mal alles steht
Hier der Code des Downloadbuttons:
VB.NET-Quellcode
- Private Sub buttonDownload_Click(sender As Object, e As EventArgs) Handles buttonDownload.Click
- If buttonDownload.Text = "Download" Then
- Dim bAbortDownload = False
- buttonDownload.Text = "Cancel"
- buttonDownload.Update()
- Dim ftpHost As String = "ftp://irgendeinserver/"
- Dim ftpUser As String = "USER"
- Dim ftpPassword As String = "password"
- Dim ftpRemoteFile As String = "/" & textFilename.Text
- Dim ftpLocalFile As String = textFilename.Text
- 'MessageBox.Show(ftpRemoteFile, "bla", MessageBoxButtons.OK)
- Try
- Dim ftpSizeUrl As String = ftpHost & ftpRemoteFile
- Dim ftpSizeReq As FtpWebRequest = System.Net.FtpWebRequest.Create(ftpSizeUrl)
- ftpSizeReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
- ftpSizeReq.KeepAlive = False
- ftpSizeReq.Method = WebRequestMethods.Ftp.GetFileSize
- Dim ftpListResponse As FtpWebResponse = ftpSizeReq.GetResponse
- Dim ftpRemoteFileSize As Long = ftpListResponse.ContentLength
- barProgress.Maximum = ftpRemoteFileSize
- Dim ftpUrl As String = ftpHost & ftpRemoteFile
- Dim ftpReq As FtpWebRequest = System.Net.FtpWebRequest.Create(ftpUrl)
- ftpReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
- ftpReq.KeepAlive = False
- ftpReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
- ftpReq.KeepAlive = False
- ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
- Using ftpResp As System.Net.FtpWebResponse = CType(ftpReq.GetResponse, FtpWebResponse)
- Using ftpRespStream As Stream = ftpResp.GetResponseStream
- Using localFileStream As New FileStream(ftpLocalFile, FileMode.Create)
- Dim buffer(2047) As Byte
- Dim read As Integer = 0
- Do
- If bAbortDownload = True Then
- Exit Do
- End If
- read = ftpRespStream.Read(buffer, 0, buffer.Length)
- localFileStream.Write(buffer, 0, read)
- barProgress.Value = localFileStream.Length
- barProgress.Update()
- Loop Until read = 0
- ftpRespStream.Close()
- localFileStream.Flush()
- localFileStream.Close()
- End Using
- ftpRespStream.Close()
- End Using
- ftpResp.Close()
- End Using
- buttonDownload.Text = "Download"
- buttonDownload.Enabled = False
- buttonDownload.Update()
- Catch ex As Exception
- MessageBox.Show("error", "Error", MessageBoxButtons.OK)
- End Try
- Else
- Dim bAbortDownload = True
- buttonDownload.Text = "Download"
- buttonDownload.Enabled = False
- buttonDownload.Update()
- End If
- Return
- End Sub