[英]Converted C# to VB.Net not adding byte to downloaded file
我已經將代碼從C#轉換為vb.net,但是在原始C#代碼有效的情況下,轉換后的代碼不會更新文件(例如:將字節添加到保存的文件中)。 我不懂C#,所以我使用了在線轉換器來轉換代碼,嘗試修復未轉換的代碼,但無濟於事。
[原始的C#代碼]
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();
}
}
[將C#轉換為vb.net代碼]
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()
我的問題是,此代碼將文件從“ sUrlToReadFileFrom”正確寫入“ sFilePathToWriteFileTo”,但zip文件始終為0字節,並且后台工作程序循環而未在文件中添加任何字節。
您不能在VB中的表達式內賦值。 也就是說,您必須在循環之前和循環結束時在循環內提取以下分配:
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
等效的VB代碼為:
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
問題是這一行:
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
它執行所謂的內聯分配 ,這意味着它在語句的中間將streamRemote.Read()
的結果分配給iByteSize
,然后返回該結果。
VB.NET不支持此功能,這可能就是您使用的轉換器刪除了它的原因。 因此, iByteSize
將始終為0,並且沒有字節寫入文件。
解決方法是在進入循環之前和循環的最后一行分別設置iByteSize
變量:
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
同樣, request
和response
變量似乎沒有在任何地方使用。 如果是這種情況,則應將其刪除。
嘗試這個:
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
InlineAssignHelper
:
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.