winform自动更新并实现文件的批量异步下载
public partial class update : Form
{
private WebClient client;
int downfilenum = 0; //已下载文件数
int downlistnum = 0;//总下载文件数
List<string> list;
private string URl;
private string fileName;
private const string applicationFile = "Setup";
public update()
{
InitializeComponent();
}
//检测网络状态
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private void update_Load(object sender, EventArgs e)
{
if (isConnected())
{
client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.Proxy = WebRequest.DefaultWebProxy;
client.Proxy.Credentials = new NetworkCredential();
updateFile();
}
else
{
lblMsg.Text = "网络连接异常,请联系管理员处理";
}
}
/// <summary>
/// 检测网络状态
/// </summary>
private bool isConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
/// <summary>
/// 获取xml文件,判断是否升级
/// </summary>
/// <param name="Dir"></param>
/// <returns></returns>
private string getSoftUpdate()
{
if (!File.Exists(Application.StartupPath + @"\\update.xml"))
{
return "nofile";
}
DateTime newDateTime;
try
{
SrmUpdate.updateSoapClient s = new SrmUpdate.updateSoapClient();
newDateTime = Convert.ToDateTime(s.GetSoftUpDateTime());//获取服务器最新更新日期
}
catch (Exception)
{
throw;
}
DateTime oldDateTime;
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\\update.xml");
oldDateTime = Convert.ToDateTime(doc.SelectSingleNode("/AutoUpdate/SoftUpdateTime").Attributes["Date"].Value);
return newDateTime >= oldDateTime ? "true" : "false";
}
/// <summary>
/// 开始下载文件
/// </summary>
private void updateFile()
{
//判断系统有无更新,如果需要升级系统,开始下载
string isUpdate = getSoftUpdate();
if (isUpdate == "true")
{
lblMsg.Text = "检测到版本有更新,准备升级";
progressBar1.Visible = true;
startDownload();
}
else if (isUpdate == "false")
{
lblMsg.Text = "无需更新,直接启动系统";
runSystem();
}
else if (isUpdate == "nofile")
{
lblMsg.Text = "缺少系统文件,请您联系管理员处理";
}
}
/// <summary>
/// 下载完成调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
downfilenum++;
if (client != null) { client.CancelAsync(); }
if (downfilenum < downlistnum) downLoadFile(list[downfilenum].ToString()); //下载剩余的
if (downfilenum == downlistnum) { init(); } //初始化
}
//下载某个文件
private void downLoadFile(string fileName)
{
downLoad(URl + fileName, Application.StartupPath + "\\temp\\" + fileName, true); //异步下载远程文件
}
/// <summary>
/// 下载进度条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
int a = e.ProgressPercentage;
progressBar1.Value = a;
lblMsg.Text = a + "% Downloading... ";
}
/// <summary>
/// HTTP下载远程文件并保存本地的函数
/// </summary>
private void downLoad(string downAddress, string savePath, Boolean Async)
{
DirectoryInfo di = Directory.GetParent(savePath);
if (!di.Exists) di.Create();
WebClient client = new WebClient(); //再次new 避免WebClient不能I/O并发
if (Async)
{ //异步下载
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(downAddress), savePath);
}
else client.DownloadFile(new Uri(downAddress), savePath);
}
/// <summary>
/// 开始下载
/// </summary>
private void startDownload()
{
SrmUpdate.updateSoapClient ws = new SrmUpdate.updateSoapClient();
URl = ws.GetSoftUpdateUrl();
list = ws.GetSoftUpdateFileList();
downlistnum = list.Count;
fileName = list[0].Substring(list[0].LastIndexOf("/") + 1, list[0].Length - list[0].LastIndexOf("/") - 1);
downLoad(URl + list[0], Application.StartupPath + "\\temp\\" + fileName, true);
}
private void init()
{
string curdir = Application.StartupPath;
DirectoryInfo theFolder = new DirectoryInfo(curdir + @"\temp");
if (theFolder.Exists)
{
copyDirectory(curdir + @"\temp", curdir);
}
if (Directory.Exists(curdir + @"\temp\")) Directory.Delete(curdir + @"\temp\", true); //删除临时目录
runSystem();
}
/// <summary>
/// 复制文件夹中的所有文件到指定文件夹
/// </summary>
/// <param name="DirectoryPath">源文件夹路径</param>
/// <param name="DirAddress">保存路径</param>
private void copyDirectory(string DirectoryPath, string DirAddress)//复制文件夹,
{
if(!Directory.Exists(DirAddress)) Directory.CreateDirectory(DirAddress);
DirectoryInfo DirectoryArray = new DirectoryInfo(DirectoryPath);
FileInfo[] Files = DirectoryArray.GetFiles();//获取该文件夹下的文件列表
DirectoryInfo[] Directorys = DirectoryArray.GetDirectories();//获取该文件夹下的文件夹列表
foreach (FileInfo theFile in Files)//逐个复制文件
{
//如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件
if (File.Exists(DirAddress + "\\" + Path.GetFileName(theFile.FullName)))
File.Delete(DirAddress + "\\" + Path.GetFileName(theFile.FullName));
//将临时文件夹的文件移到应用程序所在的目录下
File.Copy(theFile.FullName, DirAddress + "\\" + Path.GetFileName(theFile.FullName));
}
foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称,并递归调用方法本身
{
copyDirectory(DirectoryPath + "\\" + Dir.Name, DirAddress + "\\" + Dir.Name);
}
}
/// <summary>
/// 启动主程序
/// </summary>
private void runSystem()
{
//启动安装程序
this.lblMsg.Text = "正在启动主程序....";
System.Diagnostics.Process.Start(Application.StartupPath + @"\SRMClientAppLoader.exe");
this.Close();
}
}
winform自动更新并实现文件的批量异步下载的更多相关文章
- winform自动更新程序实现
一.问题背景 本地程序在实际项目使用过程中,因为可以操作电脑本地的一些信息,并且对于串口.OPC.并口等数据可以方便的进行收发,虽然现在软件行业看着动不动都是互联网啊啥的,大有Web服务就是高大上的感 ...
- Winform自动更新组件分享
作者:圣殿骑士 出处:http://www.cnblogs.com/KnightsWarrior/ 关于作者:专注于微软平台项目架构.管理和企业解决方案.自认在面向对象及面向服务领域有一定的造诣,熟悉 ...
- VB winform自动更新 笔记
看网上各种自动更新方法,最后自己找了个比较简单的,在此做个笔记. 服务器上的共享盘里存放生成的可执行文件和XML格式的配置: <?xml version="1.0" enco ...
- C# winform自动更新 (附 demo下载)
随着需求的变化,如果Server每次更新出新的内容,Client都要重新安装的话. 太过于复杂化. 所以自动更新是很有必要的. 一..NET自带的更新方式 以服务器端为主 (自动更新,微软爸 ...
- winform自动更新之AutoUpdater.NET
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zhaobw831/article/details/82226291使用AutoUpdater.NET ...
- [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件
1.项目设置 build settings ->build options ->Scan all source files and Includes = YES 2.加入script (1 ...
- Winform(C#.NET)自动更新组件的使用及部分功能实现
声明:核心功能的实现是由园子里圣殿骑士大哥写的,本人是基于他核心代码,按照自己需求进行修改的. 而AutoUpdaterService.xml文件生成工具是基于评论#215楼 ptangbao的代 ...
- Winform开发框架之通用自动更新模块(转)
在网络化的环境中,特别是基于互联网发布的Winform程序,程序的自动更新功能是比较重要的操作,这样可以避免挨个给使用者打电话.发信息通知或者发送软件等,要求其对应用程序进行升级.实现程序的自动更新, ...
- WinForm应用程序中实现自动更新功能
WinForm应用程序中实现自动更新功能 编写人:左丘文 2015-4-20 近来在给一客户实施ECM系统,但他们使用功能并不是我们ECM制造版提供的标准功能,他们要求对系统作一些定制功能,为了避免因 ...
随机推荐
- MVC应用程序显示上传的图片
MVC应用程序显示上传的图片 前两篇<MVC应用程序实现上传文件>http://www.cnblogs.com/insus/p/3590907.html和<MVC应用程序实现上传文件 ...
- SQL计算年代差
1.用datediff函数 select datediff(yyyy,StuBirthday,getdate())>17 2.用year函数 select (year(getdate()-yea ...
- Shards
跟我一起云计算(5)——Shards 什么是sharding Sharding的基本思想就要把一个数据库切分成多个部分放到不同的数据库 (server)上,从而缓解单一数据库的性能问题.不太严格的 ...
- Moq让单元测试变得更简单
[ASP.Net MVC3 ]使用Moq让单元测试变得更简单 前几天调查完了unity.现在给我的任务是让我调查Moq. 以下是自己找了资料,总结并实践的内容.如果有表述和理解错误的地方.恳请指正. ...
- Private和Protected方法
.NET中如何测试Private和Protected方法 TDD是1)写测试2)写通过这些测试的代码,3)然后重构的实践.在,NET社区中, 这个概念逐渐变得非常流行,这归功于它所增加的质量保证. ...
- 完整显示当前日期和时间的JS代码(2007年2月25日星期日正午12:42:48)
代码演示效果为“2007年2月25日星期日正午12:42:48”. 使用方法:将下面的JS代码放到你想要显示的页面中(支持HTML页面),然后在你想要显示时间的位置插入下面的代码即可 <div ...
- 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)
技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...
- ACE 容器之三 ACE_Unbounded_Queue的使用
以下代码演示了如何在ACE_Unbounded_Queue这个队列容器中存储具体的数据元素和数据元素的指针. // ACEqueue.cpp : Defines the entry point for ...
- ssh三大框架,三层架构 整合测试!完整分页代码,JdbcTemplate等测试,存储过程调用,留着以后复习吧
下载地址:http://download.csdn.net/detail/liangrui1988/5760453
- springMVC之数据传递
在前面介绍过从controller中向页面中传数据,可以通过HttpServletRequest进行setAttribute可以将数据放入request中.并且可以在jsp页面使用el表达式获取数据. ...