網頁 貼吧 文章 作者 工作  
網頁搜尋
 
 愛PO吧 >> 史萊姆好玩遊戲 >> 瀏覽文章
回覆 加入我的最愛 與好友分享

C#,從網站上下載檔案並且顯示進度列

本被文章 0 次, 共有回覆 9  
0
 
0
利用不同的thread來完成,系統優化.清理.修復應用工具顯示進度列的xyz軟體補給站方式,其範例程式如下所示:在xaml中建立如下元件 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.IO;using System.Net;using System.Threading; namespace DemoWebFDownload{ /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { private double _TOTALSIZE = 0; public Window1() { InitializeComponent(); progressBar1.Value = 0; _TOTALSIZE = 0; } private void button1_Click(object sender, RoutedEventArgs e) { if (textBox1.Text == "xxx.zip") { ThreadStart ts = new ThreadStart(Test);
Thread t = new Thread(ts); t.Start(); } } public void Test() { double MAXSIZE=0; string url = "http://xxx/xxx/xxx.zip"; HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Console.WriteLine("總大小:"+httpResponse.ContentLength); MAXSIZE=(int)httpResponse.ContentLength; System.IO.Stream dataStream = httpResponse.GetResponseStream(); byte[] buffer = new byte[8192]; FileStream fs = new FileStream("C:\\xxx.zip", FileMode.Create, FileAccess.Write); int size = 0; do { size = dataStream.Read(buffer, 0, buffer.Length); _TOTALSIZE += size; Console.WriteLine("目前下載大小:" + _TOTALSIZE); Console.WriteLine("總大小:" + MAXSIZE); Console.WriteLine("目前百分比:" + _TOTALSIZE / MAXSIZE); double svalue = _TOTALSIZE / MAXSIZE * 100; showvalue(svalue); //pb.Value = _TOTALSIZE / MAXSIZE * 100; Console.WriteLine("value:" + svalue); if (size > 0) fs.Write(buffer, 0, size); } while (size > 0); fs.Close(); httpResponse.Close(); Console.WriteLine("Done at " + DateTime.Now.ToString("HH:mm:ss.fff")); } delegate void SetEventCallback(double value); public void showvalue(double value) { if (this.Dispatcher.Thread == Thread.CurrentThread) { progressBar1.Value = value; } else { SetEventCallback d = new SetEventCallback(showvalue); this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, d, value); } } }}

逛上一篇:   逛下一篇:

作者: ebqukpae
  (2010-01-26 15:18)
推薦文章: 將本文章推薦到【百度收藏】 將本文章推薦到【YouPush】 將本文章推薦到【udn共享書籤】 將本文章推薦到【Fiigo】書籤

 本文章共有回覆 9 篇,分 1 頁
 聲明:以上內容不代表本站立場,且內容由網友發表提供,若有爭議或違法由發表者承擔,本站將不負責連帶責任,謝謝。

 IPoBar  愛PK  愛遊戲  愛online
新手教學 客服中心 站務公告 交換連結 合作提案 關於我們
 
版權所有©ipobar Ltd., All Rights Reserved.
論壇內會員言論僅代表個人觀點,不代表本站同意其說法,本討論區不承擔由該言論所引起的法律責任