//检查更新
private bool UpdateCheck()
{
Logger.Debug("开始更新");
//if (DebugHelper.IsDebug)
//{
// return true;
//}
bool updateCheck = Converter.Convert<bool>(ConfigurationManager.AppSettings["Update.Check"]);
//如果配置文件中关闭了更新功能,则不执行任何更新的动作
if (!updateCheck)
{
return true;
}
string baseDir = System.AppDomain.CurrentDomain.BaseDirectory;
string url = ConfigurationManager.AppSettings["Update.Server"];
// 用ProcessStartInfo启动一个exe程序
string args = string.Format("{0} {1} {2}", ToBase64String(url), ToBase64String(baseDir), ToBase64String(baseDir + "Starter.exe"));
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = baseDir + "Updater.exe";
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
Logger.Debug("启动Update.exe:");
//启动调用
using (Process process = Process.Start(start))
{
//接收控制台输出的消息
process.OutputDataReceived += (s1, e1) =>
{
if (e1 != null && e1.Data != null && e1.Data.Equals("Update"))
{
Process[] p = System.Diagnostics.Process.GetProcessesByName("Starter");
p[0].Kill();
}
};
process.BeginOutputReadLine();
//等待退出
process.WaitForExit();
}
#region 开始执行更新程序 wupan
//Process process = new Process();
//process.StartInfo.FileName = baseDir + "Updater.exe";
//process.StartInfo.Arguments = args;
//process.StartInfo.UseShellExecute = false;
//process.StartInfo.RedirectStandardOutput = true;
//process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
//{
// // Prepend line numbers to each line of the output.
// if (e != null && e.Data != null && e.Data.Equals("Update"))
// {
// Process[] p = System.Diagnostics.Process.GetProcessesByName("Starter");
// p[0].Kill();
// }
//});
启动
//process.Start();
开始进行异步读取工作
//process.BeginOutputReadLine();
无限期等待关联进程退出
//process.WaitForExit();
释放所有关联资源
//process.Close();
#endregion
return true;
}
/// <summary>
/// 检查更新程序
/// </summary>
/// <returns></returns>
private void CheckUpdateExe()
{
string address = ConfigurationManager.AppSettings["Update.Server"] + "/Updater.exe";
string ServerPath = AppDomain.CurrentDomain.BaseDirectory + "TemporaryFiles" + "/Updater.exe";
string LocalPath = AppDomain.CurrentDomain.BaseDirectory + "Updater.exe";
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(ServerPath));
WebClient webClient = new WebClient();
webClient.DownloadFile(address, ServerPath);
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(LocalPath));
if (MD5(LocalPath) != MD5(ServerPath))
{
//不是文件夹即复制文件,true表示可以覆盖同名文件
File.Copy(ServerPath, LocalPath, true);
}
DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "TemporaryFiles");
di.Delete(true);//删除文件夹
}