zoukankan      html  css  js  c++  java
  • 转载:C# 文件下载(WebClient)

    C# 文件下载(WebClient)

    http://www.cnblogs.com/lorn/archive/2007/12/09/988507.html

    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);
         }
    }

  • 相关阅读:
    HDU 4825 Xor Sum
    Linux下使用Crontab定时执行脚本
    HDU 4824 Disk Schedule
    Educational Codeforces Round 26 D. Round Subset 动态规划
    POJ 1833 排列
    HDU 1716 全排列
    HDU 1027 全排列
    Educational Codeforces Round 26 A C 之Python
    ACM输入输出之python
    标准C程序设计七---102
  • 原文地址:https://www.cnblogs.com/chinaontology/p/1220991.html
Copyright © 2011-2022 走看看