我已经将代码从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字节,后台工作程序循环而不向文件添加任何字节。
答案 0 :(得分:1)
您无法在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
答案 1 :(得分:1)
问题在于这一行:
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
变量似乎无法在任何地方使用。如果是这种情况,您应该删除它们。
答案 2 :(得分:0)
试试这个:
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