如何以编程方式更新始终运行并使用 msix 部署的 WinForms 应用程序?

如何解决如何以编程方式更新始终运行并使用 msix 部署的 WinForms 应用程序?

我有一个应用程序配置为在启动时运行。它没有用户界面,它会在用户登录时进入系统托盘并整天做它的事情。

应用程序应该在用户不知情的情况下自动更新,我不想打扰用户。我怎样才能用 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;
            }

解决方法

解决方案在此页面中: 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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?