C# 文件下载(WebClient)


private void DownLoadFile()
{
    try
    {
        WebClient client = new WebClient();
        if (!Directory.Exists(Application.StartupPath + @""update"))
        {
            Directory.CreateDirectory(Application.StartupPath + @""update");
        }
        for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
        {
            string fileName = this.listView1.Items[i].SubItems[0].Text;
            string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
            string file = Application.StartupPath + """update""" + fileName;
            this.label2.Text = "正在下载:" + fileName;

            Stream myStream = client.OpenRead(url);
            byte[] bBuffer = new byte[10240];
            int nRealCount = 0;
            DateTime dtStart = DateTime.Now;
            TimeSpan ts;

            int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
            int cur = 0;
            progressBar1.Value = 0;
            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
            //do  
            //{                                                
            //    dtStart = DateTime.Now;  
            //    nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);  
            //    if(nRealCount > 0)  
            //    {  
            //        fs.Write(bBuffer, 0, nRealCount);  
            //    }  
            //    ts = DateTime.Now - dtStart;  
            //    cur = cur + nRealCount;  
            //    double pe = cur;  
            //    pe = pe * 100 / lenth;  
            //    int va = Convert.ToInt32(pe);
            //    progressBar1.Value = va;                                              
            //    this.label2.Text = "正在下载:" + fileName  + "       " + cur / 1024 + "   KB/" + lenth / 1024 + "   KB";  
            //    MessageBox.Show("" + ts.Milliseconds + "     " + ts.TotalMilliseconds);  
            //   //   this.label2.Text = "正在下载:" + fileName + "       " + cur /1024 + "   KB/" + lenth / 1024 + "   KB" + "     下载速度:"+((double )nRealCount/(int)(ts.TotalMilliseconds/1000))+"   kb/s";  
            //   //   计算流量可以用nRealCount / ts.Seconds来进行估算  
            //}  
            int value = 0;
            int counter = 1;
            do
            {
                if (counter % 10 == 0)
                {
                    ts = DateTime.Now - dtStart;

                    int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024;

                    this.label3.Text = "速度:" + perSecondsValue + "   k/s";
                    dtStart = DateTime.Now;
                    value = 0;
                 }

                 nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
                 if (nRealCount > 0)
                 {
                     fs.Write(bBuffer, 0, nRealCount);
                 }
                 cur = cur + nRealCount;
                 double pe = cur;
                 pe = pe * 100 / lenth;
                 int va = Convert.ToInt32(pe);
                 progressBar1.Value = va;

                 this.label2.Text = "正在下载:" + fileName + "  " + cur / 1024 + " KB/" + lenth / 1024 + " KB";

                 value += nRealCount;
                 counter++;
                }
                    while (nRealCount > 0);

                    fs.Close();
                    //   client.DownloadFile(url, file);  
                    this.listView1.Items[i].SubItems.Add("已下载");
             }

             this.progressBar1.Visible = false;
             this.label3.Visible = false;
             this.label2.Text = "正在更新文件,请稍后...";
             string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @""update");

             foreach (string str in fileDir)
             {
                 int pos = str.LastIndexOf(@""");
                 string FileName = str.Substring(pos);
                 string FilePath = Application.StartupPath + FileName;
                 File.Copy(str, FilePath, true);
             }

             this.label2.Text = "程序更新已完成!";

             this.label1.Text = this.finish;
             this.btnNext.Enabled = false;
             this.listView1.Visible = false;
             this.btnExit.Text = "完成(&F)";
             step++;
     }
     catch (Exception errMsg)
     {
         MessageBox.Show(errMsg.Message);
     }
}

posted @ 2007-12-09 18:44  Lorn  阅读(5436)  评论(0编辑  收藏  举报