1

我正在尝试转换我在另一个站点上找到的 C# 脚本:http: //www.codeproject.com/Tips/307548/Resume-Suppoert-Downloading

到目前为止,我已经设法转换了其中的大部分内容,但是一旦我运行程序,它就会创建一个只有 0 字节的文件。当在 C# 中运行时,它工作得很好,所以我知道它一定是我的转换技能的问题(或者在这种情况下缺乏)。

这是 C#

        static void DownloadFile(string sSourceURL, string sDestinationPath)
    {
        long iFileSize = 0;
        int iBufferSize = 1024;
        iBufferSize *= 1000;
        long iExistLen = 0;
        System.IO.FileStream saveFileStream;
        if (System.IO.File.Exists(sDestinationPath))
        {
            System.IO.FileInfo fINfo =
               new System.IO.FileInfo(sDestinationPath);
            iExistLen = fINfo.Length;
        }

        if (iExistLen > 0)
            saveFileStream = new System.IO.FileStream(sDestinationPath,
              System.IO.FileMode.Append, System.IO.FileAccess.Write,
              System.IO.FileShare.ReadWrite);
        else
            saveFileStream = new System.IO.FileStream(sDestinationPath,
              System.IO.FileMode.Create, System.IO.FileAccess.Write,
              System.IO.FileShare.ReadWrite);

        System.Net.HttpWebRequest hwRq;
        System.Net.HttpWebResponse hwRes;
        hwRq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sSourceURL);
        hwRq.AddRange((int)iExistLen);
        System.IO.Stream smRespStream;
        hwRes = (System.Net.HttpWebResponse)hwRq.GetResponse();
        smRespStream = hwRes.GetResponseStream();

        iFileSize = hwRes.ContentLength;

        int iByteSize;
        byte[] downBuffer = new byte[iBufferSize];

        while ((iByteSize = smRespStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
        {
            saveFileStream.Write(downBuffer, 0, iByteSize);
        }
    } 

现在我的 VB.NET

        Dim sSourceURL As String = TextBox1.Text
    Dim sDestinationPath As String = TextBox2.Text

    Dim iFileSize As Long = 0
    Dim iBufferSize As Integer = 1024
    iBufferSize *= 1000
    Dim iExistLen As Long = 0
    Dim saveFileStream As System.IO.FileStream

    If System.IO.File.Exists(sDestinationPath) Then
        Dim fINfo As New System.IO.FileInfo(sDestinationPath)
        iExistLen = fINfo.Length
    End If

    If iExistLen > 0 Then
        saveFileStream = New System.IO.FileStream(sDestinationPath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
    Else
        saveFileStream = New System.IO.FileStream(sDestinationPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
    End If

    Dim hwRq As System.Net.HttpWebRequest
    Dim hwRes As System.Net.HttpWebResponse
    hwRq = DirectCast(System.Net.HttpWebRequest.Create(sSourceURL), System.Net.HttpWebRequest)
    hwRq.AddRange(CInt(iExistLen))
    Dim smRespStream As System.IO.Stream
    hwRes = DirectCast(hwRq.GetResponse(), System.Net.HttpWebResponse)
    smRespStream = hwRes.GetResponseStream()

    iFileSize = hwRes.ContentLength

    Dim iByteSize As Integer
    Dim downBuffer As Byte() = New Byte(iBufferSize) {}

    While ((smRespStream.Read(downBuffer, 0, downBuffer.Length))) > 0
        saveFileStream.Write(downBuffer, 0, iByteSize)
    End While

我相信我遇到的问题在这段代码中:

    While ((smRespStream.Read(downBuffer, 0, downBuffer.Length))) > 0
        saveFileStream.Write(downBuffer, 0, iByteSize)
    End While

谢谢大家的时间:)

4

1 回答 1

0

我认为您缺少对 iByteSize 的分配,该分配被检查为大于 0。

所以是这样的:

While (iByteSize = (smRespStream.Read(downBuffer, 0, downBuffer.Length))) > 0
   saveFileStream.Write(downBuffer, 0, iByteSize)
End While

编辑:可能是这样的,但我没有在本地测试过!

iByteSize = (smRespStream.Read(downBuffer, 0, downBuffer.Length))
While (iByteSize > 0)
   saveFileStream.Write(downBuffer, 0, iByteSize)
   iByteSize = (smRespStream.Read(downBuffer, 0, downBuffer.Length))
End While
于 2012-10-30T11:45:22.077 回答