本帖最后由 ikemoto2011 于 2012-12-11 15:25:48 编辑

解决方案 »

  1.   


            void updateUI(int downSize)
     {
         pbDown.Value = downSize;     if (downSize == pbDown.Maximum)
         {
             timer1.Enabled = false;
             lbSpeed.Text = ((pbDown.Maximum / 1024) / (DateTime.Now.Subtract(timeStart).Seconds)) + "KB/S";
             lbtime.Text  = DateTime.Now.Subtract(timeStart).Seconds.ToString () + "s";
             lbsize.Text  = (downSize/1024/1024).ToString ()+"M";
             
             lbInfo.Text = "下载完毕";
                    if (!Directory.Exists(filePath))
             {
                 Directory.CreateDirectory(filePath);
             }
             
             string file = filePath + DateTime.Now.ToLongDateString() + ".txt";         string content ="\r\n"+ "下载地址:" + tbFile.Text.ToString() + "\r\n" + "下载平均速度:" + lbSpeed.Text.ToString() + "\r\n"
                 + "下载所需时间:"  +
                 lbtime.Text.ToString()  +"\r\n"+ "文件大小:" + lbsize.Text.ToString() ;}这段是 updateUI  方法  
       然后  再     public int  returnstr(string tbfilestr)
            {
                int num = 0;
                if (string.IsNullOrEmpty(tbfilestr))
                {
                    MessageBox.Show("请输入需要下载的文件.");
                    return 0 ;
                }            timer1.Interval = 100;
                lbInfo.Text = "下载文件中...";
               // btnDownload.Enabled = false;            Application.DoEvents(); // 刷新界面            curDownSize = 0;
                lastDownSize = 0;            /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时)
                timer1.Enabled = true;            try
                {
                    WebRequest httpRequest = WebRequest.Create(tbfilestr);
                    httpResponse = httpRequest.GetResponse();
                    pbDown.Maximum = (int)httpResponse.ContentLength;
                }
                catch (Exception _ex)
                {
                    MessageBox.Show(_ex.Message);
                    lbInfo.Text = "点击按钮开始下载";
                    // button2.Enabled = true;
                    return 0;
                }            timeStart = DateTime.Now;
                object locker = new object();
                //lock (locker)
                //{                /// 开启文件本地保存流
                ///    using (System.IO.StreamWriter sw = System.IO.File.AppendText(logFile))             fileStream = new System.IO.FileStream(tbfilestr.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite);
                
                    /// 开启下载进程
                    downloadThread = new Thread(new ThreadStart(downloadFile));
                    downloadThread.IsBackground = true;
                    downloadThread.Start();
            
                //}
                num = 1;
                return num;
                       }     private void button5_Click(object sender, EventArgs e)
         {         //returnstr(tbFile.Text.ToString());
             //if (label5.Text == "地址1下载完成")
             //{
             //    returnstr(tbFile2.Text.ToString());         //}         //returnstr(tbFile.Text.ToString());           }     private void btnDownload2_Click(object sender, EventArgs e)
         {
             if (string.IsNullOrEmpty(tbFile2.Text.Trim()))
             {
                 MessageBox.Show("请输入需要下载的文件.");
                 return;
             }         timer1.Interval = 100;
             lbInfo.Text = "下载文件中...";
           //  btnDownload2.Enabled = false;
             Application.DoEvents(); // 刷新界面         curDownSize = 0;
             lastDownSize = 0;         /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时)
             timer1.Enabled = true;         try
             {
                 WebRequest httpRequest = WebRequest.Create(tbFile2.Text.Trim());
                 httpResponse = httpRequest.GetResponse();
                 pbDown.Maximum = (int)httpResponse.ContentLength;
             }
             catch (Exception _ex)
             {
                 MessageBox.Show(_ex.Message);
                 lbInfo.Text = "点击按钮开始下载";
                 btnDownload2.Enabled = true;
                 return;
             }         timeStart = DateTime.Now;
             object locker = new object();
             lock (locker)
             {             /// 开启文件本地保存流
                 fileStream = new System.IO.FileStream(tbFile2.Text.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite );             /// 开启下载进程
                 downloadThread = new Thread(new ThreadStart(downloadFile2));
                 downloadThread.IsBackground = true;
                 downloadThread.Start();
             }
         }     private void btnDownload3_Click(object sender, EventArgs e)
         {
             if (string.IsNullOrEmpty(tbFile3.Text.Trim()))
             {
                 MessageBox.Show("请输入需要下载的文件.");
                 return;
             }         timer1.Interval = 100;
             lbInfo.Text = "下载文件中...";
            // btnDownload3.Enabled = false;
             Application.DoEvents(); // 刷新界面         curDownSize = 0;
             lastDownSize = 0;         /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时)
             timer1.Enabled = true;         try
             {
                 WebRequest httpRequest = WebRequest.Create(tbFile3.Text.Trim());
                 httpResponse = httpRequest.GetResponse();
                 pbDown.Maximum = (int)httpResponse.ContentLength;
             }
             catch (Exception _ex)
             {
                 MessageBox.Show(_ex.Message);
                 lbInfo.Text = "点击按钮开始下载";
                 btnDownload.Enabled = true;
                 return;
             }         timeStart = DateTime.Now;
             object locker = new object();
             lock (locker)
             {             /// 开启文件本地保存流
                 fileStream = new System.IO.FileStream(tbFile3.Text.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite);             /// 开启下载进程
                 downloadThread = new Thread(new ThreadStart(downloadFile3));
                 downloadThread.IsBackground = true;
                 downloadThread.Start();
             }     }
    调用上面 2个方法
      

  2.   

    看看是不是FileMode和FileAccess没有设置正确?
      

  3.   

     while ((bufferSize = netStream.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0) 
    一直在这句话报错 
     单个手动执行button按钮是可以的
      但是写循环 分步执行button  报错了
      流不支持io并发读或写操作 
      

  4.   

    http流不支持读写
    new StreamReader(netStream)
    这样new一下就可以读写了
      

  5.   

    麻烦说下  在哪个地方new 下 
     我有点迷茫
      

  6.   


    netStream这个对象不要直接用,new StreamReader(netStream)这样把它放到一个新的StreamReader对象里,用这个新对象读数据,放在while外面