private void DownUpdateFile()
{
KillMainAppRun();
string temp = string.Empty;
string temp2 = string.Empty;
string[] keys = fileNameDic.Keys.ToArray();
for (int i = 0; i < keys.Length; i++)
{
temp = updateUrl + "//" + keys[i];
temp2 = tempUpdatePath + "//" + keys[i];
WebClient webClient = new WebClient();
if (webClient.IsBusy)
{
webClient.CancelAsync();
}
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
webClient.DownloadFileAsync(new System.Uri(temp), temp2, keys[i]);
}
}
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
UpdateProgressUI(e.ProgressPercentage);
string text = string.Format("正在下载{0},完成进度{1}/{2}(字节)", e.UserState.ToString(), e.BytesReceived, e.TotalBytesToReceive);
UpdateLableText(text);
Thread.Sleep(800);
}
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
UpdateLableText("下载被取消!");
else
{
UpdateLableText("下载完成!");
}
}
我这里其实做的是一个自动升级程序(这里使用webclient 异步更新数据 )只贴了部分代码;
大体思路就是利用 本地和服务器都存在一个程序升级xml 文件,比较更新时间 来确定需要下载那些文件
xml 大体格式如下
<?xml version="1.0" encoding="gb2312"?>
<AutoUpdater>
<description>Application autoUpdate</description>
<Updater>
<Url>http://192.168.167.85/WebService/SoftUpdate</Url>
<LastUpdateTime>2008-09-05</LastUpdateTime>
</Updater>
<Application applicationId="ForestryPDA">
<EntryPoint>ForestryPDA</EntryPoint>
<Version>1.0.0.0</Version>
</Application>
<Files>
<File Ver="1.0.0.0" Name="test.txt"/>
<File Ver="1.0.0.0" Name="lgms.ico"/>
<File Ver="1.0.0.0" Name="FormUI.exe"/>
</Files>
</AutoUpdater>
xml 里面放服务ip地址,有可能服务器地址有变化,
还有 需要下载的文件列表
大家在写过程当中 有可能提示大家web不支持并发io操作,这里要注意创建多个webclient对象,不会出现这种情况了!
这里有一篇 使用webservcie 来做的自动升级
http://blog.csdn.net/zhoujun2008/archive/2009/08/31/4503179.aspx
没细看.