示例#1
0
        protected async virtual Task <string> DownloadUpdateAsync(ApplicationInfo appInfo)
        {
            if (appInfo == null)
            {
                throw new ArgumentNullException("appInfo");
            }

            DirectoryInfo dir = new DirectoryInfo(UpdaterPaths.GetTempUpdatesFolder());

            if (!dir.Exists)
            {
                dir.Create();
            }

            string pathToCopyUpdate = Path.Combine(dir.FullName, appInfo.Version.Md5hash);

            using (var operationScope = new OperationScope(currentOperation, OperationStatus.Downloading))
            {
                try
                {
                    using (var webClient = new WebClient())
                    {
                        using (var stream = await webClient.OpenReadTaskAsync(UpdaterPaths.GetUpdatePath(appInfo.Version.Path)))
                        {
                            int bytesTotal = Convert.ToInt32(webClient.ResponseHeaders["Content-Length"]);

                            using (FileStream fs = new FileStream(pathToCopyUpdate, FileMode.Create))
                            {
                                int    bytesRead   = 0;
                                int    bytesLoaded = 0;
                                byte[] buffer      = new byte[4096];

                                do
                                {
                                    bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                                    fs.Write(buffer, 0, bytesRead);

                                    bytesLoaded += bytesRead;

                                    double progress = (double)bytesLoaded / bytesTotal * 100;
                                    ProgressReport((int)progress);
                                }while (bytesRead > 0);
                            }
                        }
                    }

                    return(pathToCopyUpdate);
                }
                catch
                {
                    throw new UpdaterException(UpdaterError.DownloadingFailed);
                }
            }
        }
示例#2
0
        private void Clear()
        {
            DirectoryInfo dir = new DirectoryInfo(UpdaterPaths.GetTempUpdatesFolder());

            try
            {
                if (dir.Exists)
                {
                    dir.Delete(true);
                }
            }
            catch
            {
            }
        }
示例#3
0
        protected async Task MoveFileAsync(FileInfo file)
        {
            try
            {
                var tempPath      = UpdaterPaths.GetTempUnpackedFolder() + "\\";
                var newFilePath   = file.FullName.Remove(file.FullName.IndexOf(tempPath), tempPath.Length);
                var directoryPath = newFilePath.Remove(newFilePath.IndexOf(file.Name));

                if (!Directory.Exists(directoryPath) && !string.IsNullOrWhiteSpace(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                using (var oldFileStream = file.OpenRead())
                {
                    using (var newFileStream = File.Create(newFilePath))
                    {
                        OnCopyingFileChanged(file.Name);

                        int    bytesRead   = 0;
                        int    bytesLoaded = 0;
                        byte[] buffer      = new byte[4096];

                        do
                        {
                            bytesRead = await oldFileStream.ReadAsync(buffer, 0, buffer.Length);

                            await newFileStream.WriteAsync(buffer, 0, bytesRead);

                            bytesLoaded += bytesRead;

                            double progress = (double)bytesLoaded / (double)file.Length * 100;
                            ProgressReport((int)progress);
                        }while (bytesRead > 0);

                        await newFileStream.FlushAsync();
                    }
                }
            }
            catch
            {
                throw new UpdaterException(UpdaterError.CopyingFailed);
            }
        }
示例#4
0
        public async virtual Task InitializeAsync(string pathToUpdatingData = null, X509Certificate2 assemblyCertificate = null)
        {
            this.assemblyCertificate = assemblyCertificate;

            // clear old data
            Clear();

            if (string.IsNullOrWhiteSpace(pathToUpdatingData))
            {
                pathToUpdatingData = UpdaterPaths.GetUpdaterDataPath();
            }
            else
            {
                pathToUpdatingData = UpdaterPaths.GetUpdatePath(pathToUpdatingData);
            }

            using (var operationScope = new OperationScope(currentOperation, OperationStatus.Getting))
            {
                var response = await loader.LoadApplicationInfoAsync(pathToUpdatingData);

                applicationInfo = response.ToArray();
                isInitialized   = true;
            }
        }
示例#5
0
        private async Task <DirectoryInfo> Unzip(string zipFile)
        {
            using (var operationScope = new OperationScope(currentOperation, OperationStatus.Unzipping))
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(UpdaterPaths.GetTempUnpackedFolder());

                    if (!dir.Exists)
                    {
                        dir.Create();
                    }

                    using (var zipFileStream = File.OpenRead(zipFile))
                    {
                        using (var archive = new ZipArchive(zipFileStream))
                        {
                            foreach (ZipArchiveEntry entry in archive.Entries)
                            {
                                if (string.IsNullOrWhiteSpace(entry.Name))
                                {
                                    continue;
                                }

                                OnUnzippingFileChanged(entry.Name);

                                var directoryPath = entry.FullName.Remove(entry.FullName.IndexOf(entry.Name));

                                if (!string.IsNullOrWhiteSpace(directoryPath))
                                {
                                    Directory.CreateDirectory(Path.Combine(dir.FullName, directoryPath));
                                }

                                string fileName = Path.Combine(dir.FullName, entry.FullName.Replace("/", @"\"));

                                using (var newFileStream = File.Create(fileName))
                                {
                                    Stream fileData = entry.Open();

                                    int    bytesRead   = 0;
                                    int    bytesLoaded = 0;
                                    byte[] buffer      = new byte[4096];

                                    do
                                    {
                                        bytesRead = await fileData.ReadAsync(buffer, 0, buffer.Length);

                                        await newFileStream.WriteAsync(buffer, 0, bytesRead);

                                        bytesLoaded += bytesRead;

                                        double progress = (double)bytesLoaded / entry.Length * 100;
                                        ProgressReport((int)progress);
                                    }while (bytesRead > 0);

                                    await newFileStream.FlushAsync();
                                }
                            }
                        }
                    }

                    return(dir);
                }
                catch
                {
                    throw new UpdaterException(UpdaterError.UnzippingFailed);
                }
            }
        }