Esempio n. 1
0
 /// <summary>
 ///     Determines whether the specified update version is valid.
 /// </summary>
 /// <param name="updateVersion">The update version to check.</param>
 public static bool IsValid(UpdateVersion updateVersion)
 {
     return IsValid(updateVersion.ToString());
 }
Esempio n. 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="UpdateManager" /> class.
        /// </summary>
        /// <param name="updateConfigurationFileUri">The URI of the update configuration file.</param>
        /// <param name="publicKey">The public key for the validity check of the update packages.</param>
        /// <param name="languageCulture">
        ///     The language culture to use. If no value is provided, the default one ("en") will be
        ///     used.
        /// </param>
        /// <param name="currentVersion">
        ///     The current version of that should be used for the update checks. This parameter has a
        ///     higher priority than the <see cref="nUpdateVersionAttribute" /> and will replace it, if specified.
        /// </param>
        /// <param name="applicationName">
        ///     The application's name.
        ///     By default, this will be derived from the entry assembly. Set this manually, if this results in a wrong application name.
        /// </param>
        /// <param name="applicationExecutablePath">
        ///     The path of the application's executable file (.exe) that should be (re)started after the update installation.
        ///     By default, this will be derived from the entry assembly. Set this manually, if this results in the wrong application being opened.
        /// </param>
        /// <remarks>
        ///     The public key can be found in the overview of your project when you're opening it in nUpdate Administration.
        ///     If you have problems inserting the data (or if you want to save time) you can scroll down there and follow the
        ///     steps of the category "Copy data" which will automatically generate the necessary code for you.
        /// </remarks>
        public UpdateManager(Uri updateConfigurationFileUri, string publicKey,
                             CultureInfo languageCulture = null, UpdateVersion currentVersion = null, string applicationName = null, string applicationExecutablePath = null)
        {
            UpdateConfigurationFileUri = updateConfigurationFileUri ??
                                         throw new ArgumentNullException(nameof(updateConfigurationFileUri));

            if (string.IsNullOrEmpty(publicKey))
            {
                throw new ArgumentNullException(nameof(publicKey));
            }
            PublicKey = publicKey;

            CultureFilePaths = new Dictionary <CultureInfo, string>();
            Arguments        = new List <UpdateArgument>();

            var projectAssembly = Assembly.GetEntryAssembly();

            if (projectAssembly == null)
            {
                throw new Exception("The entry assembly could not be determined.");
            }

            var nUpdateVersionAttribute =
                projectAssembly.GetCustomAttributes(false).OfType <nUpdateVersionAttribute>().SingleOrDefault();

            _productName = applicationName ?? projectAssembly.GetName().Name;

            if (applicationExecutablePath != null)
            {
                _executablePath = applicationExecutablePath;
            }
            else
            {
                _executablePath = projectAssembly.Location;

                // It may happen that the calling assembly is not the .exe, but another library. This is the case in .NET Core 6, for example.
                // We try to fix the path automatically. If this still does not work, the user can use the ExecutableFilePath property to set the path manually.
                if (!_executablePath.EndsWith(".exe"))
                {
                    _executablePath = Path.Combine(Path.GetDirectoryName(_executablePath) ?? string.Empty, $"{_productName}.exe");
                }
            }

            _startupPath = Path.GetDirectoryName(_executablePath);
            _applicationUpdateDirectory = Path.Combine(Path.GetTempPath(), "nUpdate",
                                                       _productName);

            // TODO: This is just there to make sure we don't create an API-change that would require a new Major version. This will be changed/removed in v5.0.
            // Before v3.0-beta8 it was not possible to provide the current version except using the nUpdateVersionAttribute.
            // In order to allow specific features, e.g. updating another application and not the current one (as it's done by a launcher), there must be a way to provide this version separately.
            // So, if an argument is specified for the "currentVersion" parameter, we will use this one instead of the nUpdateVersionAttribute.
            if (currentVersion != null)
            {
                CurrentVersion = currentVersion;
            }
            else
            {
                // Neither the nUpdateVersionAttribute nor the additional parameter argument was provided.
                if (nUpdateVersionAttribute == null)
                {
                    throw new ArgumentException(
                              "The version string couldn't be loaded because the nUpdateVersionAttribute isn't implemented in the executing assembly and no version was provided explicitly.");
                }

                CurrentVersion = new UpdateVersion(nUpdateVersionAttribute.VersionString);
            }

            // TODO: This is just there to make sure we don't create an API-change that would require a new Major version. This will be changed/removed in v4.0.
            // Before v3.0-beta5 it was not possible to use custom languages due to a mistake in the architecture. So we can be pretty sure that nobody specifies a custom CultureInfo in the constructor.
            // We only need these two lines for those who specified one of the implemented CultureInfos here as they shouldn't have to change anything when updating to v3.0-beta5.
            // Nevertheless, it's therefore possible to use custom CultureInfos just by leaving the optional parameter "null" and specifying the culture using the corresponding properties. So, both cases are covered with that solution.
            if (languageCulture != null && LocalizationHelper.IsIntegratedCulture(languageCulture, CultureFilePaths))
            {
                LanguageCulture = languageCulture;
            }
            else
            {
                throw new ArgumentException($"The culture \"{languageCulture}\" is not defined.");
            }

            if (UseCustomInstallerUserInterface && string.IsNullOrEmpty(CustomInstallerUiAssemblyPath))
            {
                throw new ArgumentException(
                          "The property \"CustomInstallerUiAssemblyPath\" is not initialized although \"UseCustomInstallerUserInterface\" is set to \"true\"");
            }
            Initialize();
        }