簡體   English   中英

下載代碼可以在Windows 7中運行,但不能在XP SP2中運行嗎?

[英]Download code works in Windows 7 but not XP SP2?

我編寫了一段簡單的代碼,可以從url下載文件。 該代碼在Windows 7中可以完美地工作,以很高的速度下載文件並維護顯示下載進度的進度條。 但是,當我從Windows XP SP2運行同一段代碼時,它會生成一個.NET IOException,這對我來說為什么無效不起作用。 它開始相同,正確完成第一次讀取,然后在第二次嘗試從流讀取時拋出以下異常:

 System.IO.IOException: Unable to read data from the transport connection: An operation on a
 socket could not be performed because the system lacked sufficient buffer space or because a
 queue was full. ---> 

 System.Net.Sockets.SocketException: An operation on a socket could not be  
 performed because the system lacked sufficient buffer space or because a queue was full
 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)

 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
 --- End of inner exception stack trace ---
 at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
 at ServicePackChk.SrvPackChk.DownLoadServicePack(Boolean bWindowsXP)
 at ServicePackChk.SrvPackChk.CheckServicePackStatus()
 at ServicePackChk.Form1.Form1_Load(Object sender, EventArgs e)
 at System.Windows.Forms.Form.OnLoad(EventArgs e)

代碼:

        Uri uri;

        if (bWindowsXP)
            uri = new Uri("http://download.microsoft.com/download/d/3/0/d30e32d8-418a-469d-b600-f32ce3edf42d/WindowsXP-KB936929-SP3-x86-ENU.exe");
        else
            uri = null;

        WebRequest req = WebRequest.Create(uri);
        WebResponse resp = req.GetResponse();

        ProgBarForm pform = new ProgBarForm();

        updateEvent += new SrvPackChk.UpdateDownloaded(pform.UpdateProgBar);

        Stream stream = resp.GetResponseStream();
        ArrayList alBytes = new ArrayList();
        int nLen = (int)resp.ContentLength;


        pform.DownLoadSize = nLen;

        pform.Show();

        byte[] byExe = new byte[nLen];

        bool bMoreToDownload = true;

        FileStream fs = new FileStream(System.IO.Path.GetTempPath() + "XPSP3.exe", FileMode.Create);

        MessageBox.Show("Saving File to " + System.IO.Path.GetTempPath() + "XPSP3.exe");

        while (bMoreToDownload)
        {
            Application.DoEvents();

            int nRead = 0;

            nRead = stream.Read(byExe, 0, nLen);

            nDownloaded += nRead;

            updateEvent(nDownloaded);

            if (nDownloaded == nLen)
            {
                bMoreToDownload = false;
            }

            fs.Write(byExe, 0, nRead);
            fs.Flush();

            Application.DoEvents();
        }

        stream.Close();

        fs.Close();

您下載的文件有300 MB以上,您嘗試一次從流中讀取。

您最好按塊讀取流,例如4MByte塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM