Wrox Programmer Forums
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2003, 02:36 PM
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dot Net BinaryWriter

I use the dot net code below to copy an .exe from our server to our
client. This uses a filestream and a BinaryWriter. For some
reason, OurProgram.exe, which is 500KB on the server, gets copied to
the client as 501KB, and then will not run. I am almost sure this
logic worked at one time, but I am not sure what changed.

Any ideas ?

Dim aScratch() As Byte
Dim aBuffer() As Byte

' make a new FileStream object, exposing our data file.
If the file exists, open it, and if it doesn't, then Create it.
Dim fs_destination As FileStream = New FileStream(destination, FileMode.OpenOrCreate)
' create the reader and writer, based on our file stream
Dim w As BinaryWriter = New BinaryWriter(fs_destination)
' initialize the WebRequest.
Dim myRequest As WebRequest = WebRequest.Create(source)
' return the response.
Dim myResponse As WebResponse = myRequest.GetResponse()
' read data into a stream
Dim reader = New BinaryReader(myResponse.GetResponseStream())
' read data into an array
On Error Resume Next
Do
   aScratch = reader.ReadBytes(4096)
   If aScratch.Length > 0 Then
      If aBuffer.Length = 0 Then
        aBuffer = aScratch
      Else
        Dim aTemp(aBuffer.Length + aScratch.Length) As Byte
        Array.Copy(aBuffer, aTemp, aBuffer.Length)
        Array.Copy(aScratch, 0, aTemp, aBuffer.Length, aScratch.Length)
        aBuffer = aTemp
      End If
   Else
      Exit Do
   End If
Loop
myResponse.Close()
' write the byte array to the file
w.Write(aBuffer)
w.Close()
fs_destination.Close()
End Sub






 
Old November 4th, 2003, 03:11 PM
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured out how to fix the problem. I just increased the reader.readbytes(size) size to be a little larger than the file I was copying. That fixes the problem. However, I am still very curious about why I had the problem in the first place, and if it was something that changed between framework 1.0 and 1.1






Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding search in Dot Net comvidyarthi Visual Studio 2005 0 March 27th, 2007 09:32 AM
Regarding search in Dot Net comvidyarthi .NET Framework 2.0 0 March 27th, 2007 09:29 AM
Can I run VB .net 2005 with DOT NET 2003 APPLICATI kadesskade BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 2 August 8th, 2006 05:14 AM
using dot net 2.0 zamankazi BOOK: ASP.NET Website Programming Problem-Design-Solution 6 October 26th, 2004 04:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.