c# 实现文件下载功能,带下载进度条,可用到软件的自动更新中

 

 

程序运行界面截图:

1、实例一个委托:

 private delegate void setText();

2、定义一个线程内方法:

public void Threadp()
        {
            try
            {
                setText d = new setText(downFile); //实例化一个委托

                this.Invoke(d); //在拥用此控件的基础窗体句柄的线程上执行指定的委托
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

3、下载文件的方法:

 public void downFile()
        {
            string updateFileUrl = textBox1.Text.Trim();
            long fileLength = 0;
            try
            {
                int readCountOnce = 5;//每次下载的字节数,该值越大,下载越快
                WebRequest webReq = WebRequest.Create(updateFileUrl);
                WebResponse webRes = webReq.GetResponse();
                fileLength = webRes.ContentLength;

                pbDownFile.Value = 0;
                pbDownFile.Maximum = (int)fileLength;
                try
                {
                    Stream srm = webRes.GetResponseStream();
                    StreamReader srmReader = new StreamReader(srm);
                    byte[] bufferbyte = new byte[fileLength];
                    int allByte = (int)bufferbyte.Length;
                    int startByte = 0;
                    while (fileLength > 0)
                    {
                        Application.DoEvents();
                        int downByte = srm.Read(bufferbyte, startByte, allByte > readCountOnce ? readCountOnce : allByte);
                        if (downByte == 0) { break; };
                        startByte += downByte;
                        allByte -= downByte;
                        pbDownFile.Value += downByte;

                        float part = (float)startByte / readCountOnce;
                        float total = (float)bufferbyte.Length / readCountOnce;
                        int percent = Convert.ToInt32((part / total) * 100);

                        this.label_precess.Text = percent.ToString() + "%";

                    }

                    string tempPath = Application.StartupPath + "//files//";
                    if (!Directory.Exists(tempPath))
                    {
                        Directory.CreateDirectory(tempPath);
                    }
                    tempPath += DateTime.Now.ToString("yyyyMMddHHmmss");
                    FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(bufferbyte, 0, bufferbyte.Length);
                    srm.Close();
                    srmReader.Close();
                    fs.Close();


                }
                catch (WebException ex)
                {
                    MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (WebException ex1)
            {
                MessageBox.Show("更新文件下载失败!" + ex1.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

4、万事具备, 只欠东风了,来一个选择文件路径的按钮(文件路径可以是网络路径)

private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = ofd.FileName;
            }
        }

5、开始下载按钮事件:

try
            {
                //string UpdateFile = textBox1.Text.Trim();
                Thread threadDown = new Thread(new ThreadStart(Threadp));
                threadDown.IsBackground = true;
                threadDown.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_______________向阳、

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值