实现客户端和服务端升级服务器的通信,根据XML文件下载升级文件
private bool DoenloadUpdateFile()
{
_downLoadSaveTempPath = _downLoadSaveFileName;
if (!Directory.Exists(_downLoadSaveTempPath))
{
Directory.CreateDirectory(_downLoadSaveTempPath);
}
int updateFileCount = _updateFileList.Count;
SetUpdateProBar((updateFileCount > 0 ? updateFileCount : 1) * 100, true);
for (int i = 0; i < updateFileCount; i )
{
SetUpdateLab(string.Format("正在更新第{0}个文件:{1}", (i 1).ToString(), _updateFileList[i]), Enum.UpdateState.downloading);
using (WebClient wcDown = new WebClient())
{
try
{
wcDown.DownloadFile(_downLoadUrl _updateFileList[i], _downLoadSaveTempPath _updateFileList[i]);
}
catch (Exception ex)
{
return false;
}
}
SetUpdateProBar(100, false);
Thread.Sleep(100);
}
return true;
}
private void ReplaceFile()
{
SetUpdateLab("正在重启程序", Enum.UpdateState.closeingmain);
OpreateMainForm(true);
Thread.Sleep(1000);
SetUpdateLab("开始替换文件", Enum.UpdateState.replacing);
try
{
foreach (string fileName in _updateFileList)
{
string moveNewPath = AppDomain.CurrentDomain.BaseDirectory fileName;
if (File.Exists(moveNewPath)) File.Delete(moveNewPath);
File.Move(_downLoadSaveTempPath fileName, moveNewPath);
}
//LrdComm.Helper.FileIO.DeleteFolder(_downLoadSaveTempPath);
}
catch (System.Exception ex)
{
SetUpdateLab(string.Format("替换文件时出错:{0}", ex.ToString()), UpdateApp.Enum.UpdateState.error);
return;
}
SaveLocalXml();
Thread.Sleep(500);
SetUpdateLab("正在重启程序", Enum.UpdateState.startmain);
OpreateMainForm(false);
Thread.Sleep(500);
SetUpdateLab("更新完成", Enum.UpdateState.success);
}
评论