在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → C# 将网络文件下载至本地

C# 将网络文件下载至本地

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:0.05M
  • 下载次数:37
  • 浏览次数:489
  • 发布时间:2017-03-12
  • 实例类别:C#文件解析和处理
  • 发 布 人:shuimu
  • 文件格式:.zip
  • 所需积分: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();
                
                }


            }
        }


    }
}


标签: 文件 解析

实例下载地址

C# 将网络文件下载至本地

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警