防止旧版本安装在较新版本上

时间:2017-08-10 16:29:11

标签: java install4j

我想设置一些版本验证,以防止最终用户在较新版本之上安装旧版本。防爆。防止在安装版本3时安装版本1。这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

您可以添加"运行脚本"行动到" Startup"安装程序的节点,用于验证已安装的版本。

以下代码段演示了如果它们是简单的整数,如何验证更新的版本:

// The value returned by context.getInstallationDirectory() will be the last 
// installation directory if the user has already installed the application
ApplicationRegistry.ApplicationInfo applicationInfo =
    ApplicationRegistry.getApplicationInfoByDir(context.getInstallationDirectory());

if (applicationInfo == null) {
  // The application has never been installed before
    return true;
}

if (Integer.parseInt(applicationInfo.getVersion()) > 
   Integer.parseInt(context.getVersion())) {
    Util.showErrorMessage("A more recent version has already been" +
    " installed in this directory");
    // By returning "false", the action will fail and the installer will quit.
    // Note that you have to set the "Failure strategy" property of your 
// "Run script" action to "Quit on error", otherwise the installer will continue.
    return false;
} else {
    return true;
}