using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
namespace Sci
{
public class UpdateTool
{
#region 文件更新逻辑
/// <summary>
/// 更新当前应用程序
/// </summary>
/// <param name="startFile">重新启动更新后的应用程序</param>
public static void Update(bool startFile = true)
{
string NameSpace = getNameSpace();
string configUrl = "http://" + ServerAddress + "/soft/" + NameSpace + "/update.txt";
string exeName = NameSpace + ".exe";
//File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "NameSpace.txt", exeName + "\r\n");
Update(configUrl, exeName, null, startFile);
}
/// <summary>
/// 更新configUrl配置文件中以perfix开头的所有文件
/// </summary>
/// <param name="configUrl">配置文件对应的网址,如:http://scimence.cn/soft/easyIcon/update.txt // update.txt由Update.exe生成</param>
/// <param name="perfix">前缀</param>
/// <param name="targeDir">本地保存路径</param>
/// <param name="startFile">打开或运行指定的文件</param>
/// <param name="deletOthers">删除本地更新目录下,其它非更新文件</param>
public static void Update(string configUrl, string perfix, string targeDir = null, bool startFile = false, bool deletOthers = false)
{
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) return;
// 在线程中执行应用程序的更新
new Thread(new ThreadStart(() =>
{
if (targeDir == null) targeDir = AppDomain.CurrentDomain.BaseDirectory;
string update_EXE = UpdatePath();
string url = "[CONFIG]" + configUrl;
string path = targeDir;
url = AddQuotation(url);
path = AddQuotation(path);
perfix = AddQuotation(perfix);
update_EXE = AddQuotation(update_EXE);
// 调用更新插件执行软件更新逻辑
String arg = url + " " + path + " " + perfix;
if (startFile) arg += " " + "START";
if (deletOthers) arg += " " + "DEL";
System.Diagnostics.Process.Start(update_EXE, arg);
})).Start();
}
/// <summary>
/// 为arg添加引号
/// </summary>
private static string AddQuotation(string arg)
{
if (arg.EndsWith("\\") && !arg.EndsWith("\\\\")) arg += "\\";
arg = "\"" + arg + "\"";
return arg;
}
static string _UpdatePath;
/// <summary>
/// 获取Update.exe文件路径信息
/// </summary>
private static string UpdatePath()
{
if (_UpdatePath == null)
{
string path = CommonDir() + "Update.exe";
GetUpdate(path);
_UpdatePath = path;
}
return _UpdatePath;
}
/// <summary>
/// 公用数据目录
/// </summary>
private static string CommonDir()
{
string dir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
if (!dir.EndsWith("\\")) dir += "\\";
dir += "scimence\\";
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
return dir;
}
/// <summary>
/// 获取当前应用的NameSpace信息
/// </summary>
private static string getNameSpace()
{
Assembly asssembly = Assembly.GetEntryAssembly();
string fullName = asssembly.FullName;
if (fullName.Contains(","))
{
fullName = fullName.Substring(0, fullName.IndexOf(","));
}
if (fullName.Contains("_v"))
{
fullName = fullName.Substring(0, fullName.IndexOf("_v"));
}
return fullName;
}
#endregion
#region Update工具
private static string ServerAddress = "www.scimence.cn";
/// <summary>
/// 获取Update工具到本地路径
/// </summary>
/// <param name="localPath">本地路径</param>
private static void GetUpdate(string localPath)
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
string url = "http://" + ServerAddress + "/soft/Update/Update.exe";
getUpdate(url, localPath);
}
}
/// <summary>
/// 从指定dataUrl载入数据,并在本地缓存
/// </summary>
private static void getUpdate(string dataUrl, string localPath)
{
try
{
// 优先从本地载入数据
if (File.Exists(localPath))
{
long lastModify = new FileInfo(localPath).LastWriteTime.Ticks;
long secondSpace = (DateTime.Now.Ticks - lastModify) / 10000000;
bool networkAvaliable = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
if (secondSpace > 86400 * 7 && networkAvaliable) // 超出7天,删除缓存文件
{
File.Delete(localPath);
}
else if (new FileInfo(localPath).Length == 0)
{
File.Delete(localPath);
}
}
// 若本地无数据,则从网址加载
if (!File.Exists(localPath))
{
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(dataUrl, localPath);
}
}
catch (Exception) { }
}
#endregion
}
}
示例:
// 更新文件:
// string[] args = {"http://scimence.cn/soft/easyIcon/easyIcon.exe", @"D:\Download\软件\easyIcon.exe", "645fe1bc2a99785460ac121d3885f2ba"};
// 更新文件,并启动:
// string[] args = {"http://scimence.cn/soft/easyIcon/easyIcon.exe", @"D:\Download\软件\easyIcon.exe", "645fe1bc2a99785460ac121d3885f2ba", "START"};
// 更新目录:(easyIcon\pic\ 到 本地路径 D:\Download\软件\ 下)
// string[] args = {"[CONFIG]http://scimence.cn/soft/easyIcon/update.txt", @"D:\Download\软件\", @"easyIcon\pic\"};
// 更新目录: (easyIcon\pic\ 到 本地路径 D:\Download\软件\easyIcon2\pic2\ 下)
// string[] args = {"[CONFIG]http://scimence.cn/soft/easyIcon/update.txt", @"D:\Download\软件\", @"easyIcon\pic\->easyIcon2\pic2\"};
// 更新目录,并删除本地easyIcon\pic\下,非CONFIG中配置的其它文件:
// string[] args = {"[CONFIG]http://scimence.cn/soft/easyIcon/update.txt", @"D:\Download\软件\", @"easyIcon\pic\", "DEL"};