加强自动更新时替换文件的能力

This commit is contained in:
大石头 2022-11-27 08:35:23 +08:00
parent a953979f93
commit ce4128d34e
4 changed files with 22 additions and 9 deletions

View File

@ -411,6 +411,7 @@ internal class MyService : ServiceBase, IServiceProvider
}
catch (Exception ex)
{
XTrace.WriteException(ex);
client.WriteErrorEvent("Upgrade", ex.ToString());
}
}

View File

@ -180,7 +180,7 @@ public class LocalStarClient
WriteLog("目标:{0}", target);
var ug = new Upgrade
var ug = new Stardust.Web.Upgrade
{
SourceFile = Path.GetFileName(url).GetFullPath(),
DestinationPath = target,

View File

@ -3,7 +3,6 @@ using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net.WebSockets;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using NewLife;
@ -778,7 +777,7 @@ public class StarClient : ApiHttpClient, ICommandClient, IEventProvider
XTrace.WriteLine("检查更新:{0}", channel);
// 清理
var ug = new Upgrade { Log = XTrace.Log };
var ug = new Stardust.Web.Upgrade { Log = XTrace.Log };
ug.DeleteBackup(".");
var rs = await UpgradeAsync(channel);

View File

@ -7,7 +7,6 @@ namespace Stardust.Web;
/// <summary>升级更新</summary>
/// <remarks>
/// 优先比较版本Version再比较时间Time。
/// 自动更新的难点在于覆盖正在使用的exe/dll文件通过改名可以解决。
/// </remarks>
public class Upgrade
@ -115,10 +114,19 @@ public class Upgrade
if (File.Exists(del)) File.Delete(del);
item.MoveTo(del);
}
catch
catch (Exception ex)
{
// 删除失败时,移动到临时目录随机文件
item.MoveTo(Path.GetTempFileName());
WriteLog(ex.Message);
try
{
// 删除失败时,移动到临时目录随机文件
item.MoveTo(Path.GetTempFileName());
}
catch (Exception ex2)
{
WriteLog(ex2.Message);
}
}
}
@ -267,7 +275,10 @@ public class Upgrade
{
item.Delete();
}
catch { }
catch (Exception ex)
{
WriteLog(ex.Message);
}
}
}
@ -297,8 +308,10 @@ public class Upgrade
{
item.CopyTo(dst.EnsureDirectory(true), true);
}
catch
catch (Exception ex)
{
WriteLog(ex.Message);
// 如果是exe/dll则先改名因为可能无法覆盖
if (/*dst.EndsWithIgnoreCase(".exe", ".dll") &&*/ File.Exists(dst))
{