亚洲午夜久久久久久噜噜噜,欧美+国产在线观看,日韩a片无码毛片免费看,爱如潮水日本免费完整版观看

C# Winform下載文件并顯示進度條

開發技術 2015/6/15

首頁要在窗體上拖progressBar、label和button三個控件,拖到適當的位置

雙擊button按紐,在里面加上代碼
private void button1_Click(object sender, EventArgs e)
{
   //點擊開始下載
   string url = "http://www.aspbc.com/1.rar";//這里換成你要下載的文件
   DownloadFile(url, @"F:\" + System.IO.Path.GetFileName(url), progressBar1, label1);
}

/// <summary>        

/// c#.net 下載文件

/// </summary>

/// <param name="URL">下載文件地址</param>

/// <param name="Filename">下載后的存放地址</param>

/// <param name="Prog">用于顯示的進度條</param>

public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)

{

   float percent = 0;

   try

   {

       System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);

       System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();

       long totalBytes = myrp.ContentLength;

       if (prog != null)

       {

           prog.Maximum = (int)totalBytes;

       }

       System.IO.Stream st = myrp.GetResponseStream();

       System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);

       long totalDownloadedByte = 0;

       byte[] by = new byte[1024];

       int osize = st.Read(by, 0, (int)by.Length);

       while (osize > 0)

       {

           totalDownloadedByte = osize + totalDownloadedByte;

           System.Windows.Forms.Application.DoEvents();

           so.Write(by, 0, osize);

           if (prog != null)

           {

               prog.Value = (int)totalDownloadedByte;

           }

           osize = st.Read(by, 0, (int)by.Length);

           percent = (float)totalDownloadedByte / (float)totalBytes * 100;

           string s2 = String.Format("{0:N2}", percent);

           label1.Text = "正在下載,下載進度" + s2 + "%";

           System.Windows.Forms.Application.DoEvents(); //必須加注這句代碼,否則label1將因為循環執行太快而來不及顯示信息

        }                

        so.Close();

        st.Close();

        //下載完成

        label1.Text = "下載完成";                

    }

    catch (System.Exception)

    {

        throw;

     }

}


中國· 上海

谷谷二維碼
添加微信咨詢

CopyRight?2009-2019 上海谷谷網絡科技有限公司 All Rights Reserved. 滬ICP備11022482號-8  

關于我們 | 聯系我們

主站蜘蛛池模板: 图木舒克市| 康定县| 桑日县| 乐业县| 枞阳县| 侯马市| 巴青县| 灵石县| 德兴市| 鸡泽县| 全椒县| 溧水县| 金乡县| 视频| 繁昌县| 钟祥市| 小金县| 新昌县| 岑溪市| 成都市| 那曲县| 花莲县| 巍山| 乌鲁木齐县| 故城县| 绥中县| 宜城市| 江陵县| 兴山县| 龙江县| 交口县| 壶关县| 丰台区| 隆林| 察雅县| 云梦县| 迁安市| 正定县| 温泉县| 富顺县| 汾阳市|