如何以编程方式更新始终运行并使用 msix 部署的 WinForms 应用程序?
Posted
技术标签:
【中文标题】如何以编程方式更新始终运行并使用 msix 部署的 WinForms 应用程序?【英文标题】:How do I update programatically a WinForms app which is always running, deployed with msix? 【发布时间】:2021-05-14 16:22:55 【问题描述】:我有一个配置为在启动时运行的应用程序。它没有用户界面,它会在用户登录时进入系统托盘并整天做它的事情。
应用程序应该会自动更新,无需用户通知,我不想打扰用户。如何使用 msix 做到这一点?
我在使用 AppInstaller 自动更新时遇到了问题,因为该应用程序一直在运行,而且它有很多问题。这段代码在某种程度上起作用。它关闭应用程序,进行更新,但更新后不启动应用程序。我需要应用程序在更新后立即运行,无需用户干预。我该怎么做?
var result = await Package.Current.CheckUpdateAvailabilityAsync();
switch (result.Availability)
case PackageUpdateAvailability.Available:
case PackageUpdateAvailability.Required:
PackageManager packagemanager = new PackageManager();
await packagemanager.UpdatePackageAsync(new Uri("path-to-file.msix"), null, DeploymentOptions.ForceApplicationShutdown);
break;
case PackageUpdateAvailability.NoUpdates:
// Close AppInstaller.
MessageBox.Show("Non-updates");
break;
case PackageUpdateAvailability.Unknown:
default:
MessageBox.Show($"No update information associated with app");
break;
【问题讨论】:
这能回答你的问题吗? Update application silently while running 不,我使用的是 msix。 那为什么你的问题包含winforms标签?如果建议的副本没有解决您的问题,那么您的问题似乎不是 about winforms,因此不应包含该标签。 @JohnG 我认为这可行,但在 msix 的上下文中,我必须部署两个包,一个用于“包装器”,另一个包用于应用程序,否则安装将失败。我正在探索的另一个选择是在 Windows 任务计划程序中添加一个任务,以便在更新后几分钟重新启动应用程序,但这太 hacky。我希望 msix 支持更简洁的解决方案。 【参考方案1】:解决方案在此页面中: https://docs.microsoft.com/en-us/windows/msix/non-store-developer-updates#automatically-restarting-your-app-after-an-update
在开始更新之前调用 RegisterApplicationRestart()。
private async Task UpdatePackage()
try
// Register the active instance of an application for restart in your Update method
uint res = RelaunchHelper.RegisterApplicationRestart(null, RelaunchHelper.RestartFlags.NONE);
PackageManager packagemanager = new PackageManager();
await packagemanager.UpdatePackageAsync(new Uri($"path-to.msix"), null, DeploymentOptions.ForceApplicationShutdown);
catch (Exception ex)
MessageBox.Show("Error in update: " + ex.ToString());
class RelaunchHelper
#region Restart Manager Methods
/// <summary>
/// Registers the active instance of an application for restart.
/// </summary>
/// <param name="pwzCommandLine">
/// A pointer to a Unicode string that specifies the command-line arguments for the application when it is restarted.
/// The maximum size of the command line that you can specify is RESTART_MAX_CMD_LINE characters. Do not include the name of the executable
/// in the command line; this function adds it for you.
/// If this parameter is NULL or an empty string, the previously registered command line is removed. If the argument contains spaces,
/// use quotes around the argument.
/// </param>
/// <param name="dwFlags">One of the options specified in RestartFlags</param>
/// <returns>
/// This function returns S_OK on success or one of the following error codes:
/// E_FAIL for internal error.
/// E_INVALIDARG if rhe specified command line is too long.
/// </returns>
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
internal static extern uint RegisterApplicationRestart(string pwzCommandLine, RestartFlags dwFlags);
#endregion Restart Manager Methods
#region Restart Manager Enums
/// <summary>
/// Flags for the RegisterApplicationRestart function
/// </summary>
[Flags]
internal enum RestartFlags
/// <summary>None of the options below.</summary>
NONE = 0,
/// <summary>Do not restart the process if it terminates due to an unhandled exception.</summary>
RESTART_NO_CRASH = 1,
/// <summary>Do not restart the process if it terminates due to the application not responding.</summary>
RESTART_NO_HANG = 2,
/// <summary>Do not restart the process if it terminates due to the installation of an update.</summary>
RESTART_NO_PATCH = 4,
/// <summary>Do not restart the process if the computer is restarted as the result of an update.</summary>
RESTART_NO_REBOOT = 8
#endregion Restart Manager Enums
【讨论】:
以上是关于如何以编程方式更新始终运行并使用 msix 部署的 WinForms 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章