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

 

 

程序运行界面截图:

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

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);
            }

 

 

你可能感兴趣的:(C#)