I've converted a code from C# to vb.net, however the converted code doesn't update the file (eg: add bytes to the saved file) while the original C# code works. I am not knowledgeable in C# so I used an online converter to convert the code, tried fixing unconverted codes but to no avail.
[original c# code]
if (serverVersion > localVersion)
{
Uri url = new Uri(sUrlToReadFileFrom);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
Int64 iSize = response.ContentLength;
Int64 iRunningByteTotal = 0;
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
{
using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
{
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
backgroundWorker1.ReportProgress(iProgressPercentage);
}
streamLocal.Close();
}
streamRemote.Close();
}
}
[converted c# to vb.net code]
If (serverVersion > localVersion) Then
Dim url As Uri = New Uri(sUrlToReadFileFrom)
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
response.Close()
Dim iSize As Int64 = response.ContentLength
Dim iRunningByteTotal As Int64 = 0
Dim client As System.Net.WebClient = New System.Net.WebClient
Dim streamRemote As System.IO.Stream = client.OpenRead(New Uri(sUrlToReadFileFrom))
Dim streamLocal As Stream = New FileStream(sFilePathToWriteFileTo, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)
Dim iByteSize As Integer = 0
Dim byteBuffer() As Byte = New Byte((iSize) - 1) {}
While (streamRemote.Read(byteBuffer, 0, byteBuffer.Length) > 0)
streamLocal.Write(byteBuffer, 0, iByteSize)
iRunningByteTotal = iRunningByteTotal + iByteSize
Dim dIndex = CType((iRunningByteTotal), Double)
Dim dTotal = CType(byteBuffer.Length, Double)
Dim dProgressPercentage As Double = (dIndex / dTotal)
Dim iProgressPercentage As Integer = CType((dProgressPercentage * 100), Integer)
bgMain.ReportProgress(iProgressPercentage)
'status.Text = "Downloading updatess " & dIndex & " of " & dTotal
End While
streamLocal.Close()
streamRemote.Close()
My issue is that, this code, writes the file from "sUrlToReadFileFrom" properly to "sFilePathToWriteFileTo" but the zip file is always 0 bytes and the Background worker loops without adding any byte to the file.
You can't assign within an expression in VB. That is, you have to extract out the folowing assignment before the loop and within the loop at the end of the loop:
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
The equivalent VB code is:
If serverVersion > localVersion Then
Dim url As New Uri(sUrlToReadFileFrom)
Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)
response.Close()
Dim iSize As Int64 = response.ContentLength
Dim iRunningByteTotal As Int64 = 0
Using client As New System.Net.WebClient()
Using streamRemote As System.IO.Stream = client.OpenRead(New Uri(sUrlToReadFileFrom))
Using streamLocal As Stream = New FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None)
Dim iByteSize As Integer = 0
Dim byteBuffer(iSize - 1) As Byte
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
Do While iByteSize > 0
streamLocal.Write(byteBuffer, 0, iByteSize)
iRunningByteTotal += iByteSize
Dim dIndex As Double = CDbl(iRunningByteTotal)
Dim dTotal As Double = CDbl(byteBuffer.Length)
Dim dProgressPercentage As Double = (dIndex / dTotal)
Dim iProgressPercentage As Integer = CInt(Math.Truncate(dProgressPercentage * 100))
backgroundWorker1.ReportProgress(iProgressPercentage)
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
Loop
streamLocal.Close()
End Using
streamRemote.Close()
End Using
End Using
The problem is this line:
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
It performs what's called inline assignment , which means that it assigns the result of streamRemote.Read()
to iByteSize
in the middle of a statement and then returns the result of that.
VB.NET doesn't support this, which probably is why the converter you have used removed it. Thus iByteSize
will always be 0, and no bytes are written to the file.
The fix is to set the iByteSize
variable separately before you enter the loop, and at the last line of the loop:
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
While iByteSize > 0
...the rest of your code...
'Put this last:
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
End While
Also, the request
and response
variables doesn't appear to be used anywhere. If this is the case you should remove them.
Try this:
If serverVersion > localVersion Then
Dim url As New Uri(sUrlToReadFileFrom)
Dim request As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
Dim response As System.Net.HttpWebResponse = DirectCast(request.GetResponse(), System.Net.HttpWebResponse)
response.Close()
Dim iSize As Int64 = response.ContentLength
Dim iRunningByteTotal As Int64 = 0
Using client As New System.Net.WebClient()
Using streamRemote As System.IO.Stream = client.OpenRead(New Uri(sUrlToReadFileFrom))
Using streamLocal As Stream = New FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None)
Dim iByteSize As Integer = 0
Dim byteBuffer As Byte() = New Byte(iSize - 1) {}
While (InlineAssignHelper(iByteSize, streamRemote.Read(byteBuffer, 0, byteBuffer.Length))) > 0
streamLocal.Write(byteBuffer, 0, iByteSize)
iRunningByteTotal += iByteSize
Dim dIndex As Double = CDbl(iRunningByteTotal)
Dim dTotal As Double = CDbl(byteBuffer.Length)
Dim dProgressPercentage As Double = (dIndex / dTotal)
Dim iProgressPercentage As Integer = CInt(dProgressPercentage * 100)
backgroundWorker1.ReportProgress(iProgressPercentage)
End While
streamLocal.Close()
End Using
streamRemote.Close()
End Using
End Using
End If
The InlineAssignHelper
:
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.