//************************************************************************
// https://github.com/yuzhengyang
// author: yuzhengyang
// date: 2017.8.1 - 2017.8.1
// desc: 程序更新工具
// Copyright (c) yuzhengyang. All rights reserved.
//************************************************************************
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Y.Utils.DelegateUtils;
using Y.Utils.IOUtils.FileUtils;
using Y.Utils.IOUtils.PathUtils;
using Y.Utils.NetUtils.FTPUtils;
using Y.Utils.NetUtils.HttpUtils;
namespace Y.Utils.UpdateUtils
{
///
/// 程序更新工具
///
public class AppUpdateTool
{
///
/// 更新
///
/// 功能名称
/// 当前版本号
/// 请求新版本地址
/// 文件下载位置
/// 文件相对位置字典
/// 下载进度回调
/// 下载进度事件数据
/// 释放进度回调
/// 释放进度事件数据
///
/// -10;//无最新版本,停止操作
/// -20;//请求服务器最新版本失败
/// -30;//新版本号格式不正确,解析失败
/// -40;//文件下载失败
/// -50;//文件释放失败
///
public int Update(string name, Version version, string url, string path, Dictionary dictionary,
ProgressDelegate.ProgressHandler downprogress = null, object downsender = null,
ProgressDelegate.ProgressHandler releaseprogress = null, object releasesender = null)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//请求最新版本信息
AppUpdateInfo info = HttpTool.Get(string.Format("{0}?name={1}", url, name));
if (info != null)
{
Version newVersion = GerVersion(info.Version);
if (newVersion == null) return -30;//新版本号格式不正确,解析失败
if (newVersion > version)
{
string file = DirTool.Combine(path, name + newVersion.ToString());
//准备更新(下载)
string downfile = Download(file, info, downprogress, downsender);
if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
{
//格式化释放文件目录
string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
//释放文件
if (FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender) > 0)
{
stopwatch.Stop();
return (int)stopwatch.Elapsed.TotalSeconds;
}
else
{
return -50;//文件释放失败
}
}
else
{
return -40;//文件下载失败
}
}
else
{
return -10;//无最新版本,停止操作
}
}
else
{
return -20;//请求服务器最新版本失败
}
}
private Version GerVersion(string s)
{
//解析最新版本号
Version version = null;
try
{
version = new Version(s);
}
catch (Exception e) { }
return version;
}
private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
{
FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
if (ftp.Download(info.FtpFile, file, progress, sender))
return file;
return null;
}
}
}