基本信息
源码名称:C# 将网络文件下载至本地
源码大小:0.05M
文件格式:.zip
开发语言:C#
更新时间:2017-03-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
网络文件获取和读取 解析
网络文件获取和读取 解析
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace DownWy { public partial class MainForm : Form { //Thread[] thrd = new Thread[4]; Thread tr = null; Threadstats[] Tstatus = new Threadstats[4]; string dir; private string[] DownFileList; public MainForm() { InitializeComponent(); dt_start.Value = DateTime.Now.AddDays(-30); dt_end.Value = DateTime.Now.AddDays(-1); for (int i = 0; i < 4; i ) { Tstatus[i] = new Threadstats(); } } private void BT_Start_Click(object sender, EventArgs e) { if (BT_Start.Text == "开始") { if (dt_end.Value.Date >= DateTime.Now.Date) { MessageBox.Show("结束日期不能超过当前日期"); return; } if (dt_end.Value.Date <= dt_start.Value.Date) { MessageBox.Show("结束日期不能小于或者等于开始日期"); return; } if (TE_Dir.Text == "" || !System.IO.Directory.Exists(TE_Dir.Text)) { MessageBox.Show("保存目录不存在!"); return; } dir = TE_Dir.Text; DownFileList = null; tr = new Thread(new ThreadStart(DownThread)); tr.Start(); BT_Start.Text = "终止"; return; } else { MainForm_FormClosed(this,null); BT_Start.Text = "开始"; } } private void DownThread() { DateTime dtbuff = dt_start.Value; for (int i = 0; i < 2; i ) { if (dtbuff > dt_end.Value) { return; } if (Tstatus[i].Isbusy == false) { Tstatus[i].Isbusy = true; Tstatus[i].dt = dtbuff; if (Tstatus[i].thrd != null && Tstatus[i].thrd.IsAlive) { Tstatus[i].thrd.Abort(); } Tstatus[i].thrd = new Thread(delegate() { DownFile(i); }); Tstatus[i].thrd.Start(); dtbuff = dtbuff.AddDays(1); System.Threading.Thread.Sleep(100); } System.Threading.Thread.Sleep(10); //让其死循环 if (i >=1) { i = -1; } } } private void DownFile(int Index) { System.IO.Stream st = null; System.IO.Stream so = null; System.Net.HttpWebRequest Myrq = null; System.Net.HttpWebResponse myrp = null; string Buff = "http://www.chinawuyuan.com/movies/"; string URL = Buff Tstatus[Index].dt.ToString("yyyyMMdd") ".wmv"; string filename = dir "/" Tstatus[Index].dt.ToString("yyyyMMdd") ".wmv"; Tstatus[Index].FileName = URL; try { Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); Myrq.Proxy = System.Net.WebProxy.GetDefaultProxy(); myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (totalBytes < 10000) { return; } Tstatus[Index].filelength = ((float)totalBytes) / 1024 / 1024; st = myrp.GetResponseStream(); 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); osize = st.Read(by, 0, (int)by.Length); Tstatus[Index].downfilelength = ((float)totalDownloadedByte) / 1024 / 1024; Tstatus [Index].percent= (float)totalDownloadedByte / (float)totalBytes * 100; } //文件显示 if (DownFileList == null) { DownFileList = new string[1]; DownFileList[0] = Tstatus[Index].FileName; } else { Array.Resize(ref DownFileList, DownFileList.Length 1); DownFileList[DownFileList.Length-1] = Tstatus[Index].FileName; } } catch (System.Exception) { //throw; } finally { if (myrp != null) { myrp.Close(); } if (Myrq != null) { Myrq.Abort(); } if (so != null) { so.Close(); } if (st != null) { st.Close(); } Tstatus[Index].Isbusy = false; //if (Tstatus[Index].thrd.IsAlive) //{ // Tstatus[Index].thrd.Abort(); //} } } struct Threadstats { public Thread thrd; public bool Isbusy; public float percent; public string FileName; public string datetime; public DateTime dt; public float filelength; public float downfilelength; } private void timer_Refresh_Tick(object sender, EventArgs e) { ll_1.Text = Tstatus[0].FileName " " Tstatus[0].downfilelength.ToString() "/" Tstatus[0].filelength.ToString(); pb_1.Value = (int)Tstatus[0].percent; ll_2.Text = Tstatus[1].FileName " " Tstatus[1].downfilelength.ToString() "/" Tstatus[1].filelength.ToString(); ; pb_2.Value = (int)Tstatus[1].percent; if (DownFileList != null && DownFileList.Length != LB_List.Items.Count) { LB_List.Items.Clear(); LB_List.Items.AddRange((object[]) DownFileList); } else { if (LB_List.Items.Count!=0) { LB_List.Items.Clear(); } } } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { if (tr != null && tr.IsAlive) { tr.Abort(); } for (int i = 0; i < 4; i ) { if (Tstatus[i].thrd != null && Tstatus[i].thrd.IsAlive) { Tstatus[i].thrd.Abort(); } } } } }