I'm attempting to use a Webclient to download files from the internet, but I am running into problems. So far I have created a simple test project to get it working correctly, the code is below:
VB Code:
Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click Const PACKET_SIZE As Integer = 1024 ^ 2 Dim ch As Char() ReDim ch(PACKET_SIZE) 'Create the webclient and open a download stream Dim WC As New Net.WebClient Dim sr As New IO.StreamReader(WC.OpenRead(txtSrc.Text)) Dim sw As New IO.StreamWriter(txtDest.Text, False, System.Text.Encoding.Default) Dim NumRead As Integer 'Download the file lblProg.Text = "Downloading File..." : Application.DoEvents() While sr.Peek >= 0 NumRead = sr.ReadBlock(ch, 0, PACKET_SIZE) sw.Write(ch, 0, NumRead) End While 'Tidy up sw.Close() : sw = Nothing sr.Close() : sr = Nothing WC.Dispose() : WC = Nothing lblProg.Text = "Operation Complete" End Sub
However, the problem that I'm having is that the webclient doesn't always download a complete file. It will download text files without a hitch, but when you try and use it on something like a zip or executable it will download only a fraction of the file - usually about two thirds.
I have a suspicion the problem lies with the StreamWriter - could it be that the streamwriter is not writing the non-printable characters, hence why it works on text files correctly?




Reply With Quote