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
|