一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net WinForm下载文件并显示下载进度条教程

时间:2022-06-25 08:33:53 编辑:袖梨 来源:一聚教程网

asp.net WinForm下载文件并显示下载进度条教程

WinForm下载文件并显示下载进度示例

 代码如下 复制代码

///


/// 显示进度
///

///
private void ProgressBar_Value(int val)
{
    progressBar1.Value = val;
    label1.Text = val.ToString() + "%";
}

///


/// 下载文件
///

///
///
///
///
private void DownloadFile(string url, string savefile, Action downloadProgressChanged, Action downloadFileCompleted)
{
    WebClient client = new WebClient();
    if (downloadProgressChanged != null)
    {
        client.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
        {
            this.Invoke(downloadProgressChanged, e.ProgressPercentage);
        };
    }
    if (downloadFileCompleted != null)
    {
        client.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e)
        {
            this.Invoke(downloadFileCompleted);
        };
    }
    client.DownloadFileAsync(new Uri(url), savefile);
}
delegate void Action(); //.NET Framework 2.0得自定义委托Action

///


/// 点击下载
///

///
///
private void button1_Click(object sender, EventArgs e)
{
    DownloadFile("http://www.111com.net/ update.zip", @"F:update.zip", ProgressBar_Value, null);
}

热门栏目