コード例 #1
0
        /// <summary>
        /// Start updating. Use events to get informations.
        /// </summary>
        public void Start()
        {
            try
            {
                IVersionParser verPar;
                if (Path.GetExtension(VersionsFile.ToString()) == ".txt")
                {
                    verPar = new VersionParserTxt();
                }

                /*else if (Path.GetExtension(VersionsFile.ToString()) == ".xml")
                 * {
                 *   verPar = new VersionParserXml();
                 * }*/
                else
                {
                    throw new InvalidOperationException("Cannot parse this type of version file.");
                }


                List <UpdateFile> availableVersions = verPar.GetMissingVersions(VersionsFile, CurrentVersion);

                foreach (var ver in availableVersions)
                {
                    OnInformation(new InformationEventArgs("Available version: " + ver.Version.ToString()));
                }

                List <string> downloadedFiles = new List <string>();

                IDownloadManager dowMan = new DownloadManager()
                {
                    Destination = OwnerOrDirectoryPath
                };
                dowMan.DownloadCompleted += (sender, args) => OnInformation(new InformationEventArgs("Downloaded and saved to: " + args.PathToFileOnComputer));
                dowMan.DownloadCompleted += (sender, args) => downloadedFiles.Add(args.PathToFileOnComputer);
                dowMan.DownloadFiles(availableVersions);

                while (availableVersions.Count != downloadedFiles.Count)
                {
                    Thread.Sleep(250);
                }

                downloadedFiles.Sort();

                var extractor = new Extract.ExtractorZip()
                {
                    Destination = OwnerOrDirectoryPath
                };

                foreach (var downloadedFile in downloadedFiles)
                {
                    extractor.Extract(downloadedFile);
                    OnInformation(new InformationEventArgs("Extracted file: " + Path.GetFileName(downloadedFile)));
                }
            }
            catch (Exception e)
            {
                OnFailure(new ErrorEventArgs(e));
            }
            finally
            {
                removeTemporaryFolder();
                OnEnd();
            }
        }