public void Rollback(Manifest manifest) { try { string filename = string.Empty; foreach (var file in manifest.ManifestFiles.Files) { filename = Path.Combine(backupFilePath, file.Source); File.Copy(filename, Path.Combine(Path.GetFullPath(manifest.Application.Location), file.Source)); } Directory.Delete(backupFilePath, true); } catch { } }
private void Backup(Manifest manifest) { try { string sourcePath = Path.GetFullPath(manifest.Application.Location); string s_filename = string.Empty; string t_filename = string.Empty; if (!Directory.Exists(backupFilePath)) { Directory.CreateDirectory(backupFilePath); } foreach (var file in manifest.ManifestFiles.Files) { t_filename = Path.Combine(backupFilePath, file.Source); s_filename = Path.Combine(sourcePath, file.Source); if (File.Exists(s_filename)) { File.Copy(s_filename, t_filename, true); } } } catch { } }
public void Activate(Manifest[] manifests) { foreach (var m in manifests) { OnActivationInitializing(new ManifestEventArgs() { Manifest = m }); Backup(m); ActivationStartedEventArgs e = new ActivationStartedEventArgs() { Manifest = m }; OnActivationStarted(e); if (e.Cancel) { Clear(); break; } else { fileCopyer.CopyAsync(m, downloader.TempPath); } } }
public void DownloadAsync(Manifest[] manifests) { foreach (var m in manifests) { downloader.DownloadAsync(m); } }
/// <summary> /// 异步下载并指定任务Id /// </summary> /// <param name="manifest"></param> /// <param name="taskId"></param> public void DownloadAsync(Manifest manifest,object taskId) { AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId); lock (userStateToLifetime.SyncRoot) { if (userStateToLifetime.Contains(taskId)) { throw new ArgumentException("参数taskId必须是唯一的", "taskId"); } userStateToLifetime[taskId] = asyncOp; } WorkerEventHandler workerDelegate = new WorkerEventHandler(DownloadWorker); workerDelegate.BeginInvoke(asyncOp, null, null); }
private void Init(Manifest manifest) { this.manifest = manifest; webClient.BaseAddress = manifest.ManifestFiles.BaseUrl; webClient.Credentials = CredentialCache.DefaultCredentials; webClient.Encoding = Encoding.UTF8; }
/// <summary> /// 同步下载 /// </summary> /// <param name="manifest"></param> public void Download(Manifest manifest) { Init(manifest); foreach (var file in manifest.ManifestFiles.Files) { string serverFileName = Path.Combine(manifest.ManifestFiles.BaseUrl, file.Source); string clientFileName = Path.Combine(tempPath, file.Source); Uri uri = new Uri(serverFileName); if (!Directory.Exists(Path.GetDirectoryName(clientFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(clientFileName)); } webClient.DownloadFile(uri, clientFileName); } }
/// <summary> /// 异步下载 /// </summary> /// <param name="manifest"></param> public void DownloadAsync(Manifest manifest) { Init(manifest); DownloadAsync(manifest,defaultTaskId); }
public void Copy(Manifest manifest, string sourcePath) { string[] sourceFiles = null; string[] targetFiles = null; GetFiles(manifest, sourcePath, out sourceFiles, out targetFiles); for (int i = 0; i < sourceFiles.Length; i++) { if (!Directory.Exists(Path.GetDirectoryName(targetFiles[i]))) { Directory.CreateDirectory(Path.GetDirectoryName(targetFiles[i])); } File.Copy(sourceFiles[i], targetFiles[i], true); } }
private void GetFiles(Manifest manifest, string sourcePath, out string[] sourceFiles, out string[] targetFiles) { sourceFiles = new string[manifest.ManifestFiles.Files.Length]; targetFiles = new string[manifest.ManifestFiles.Files.Length]; string path = Path.GetFullPath(manifest.Application.Location); for (int i = 0; i < manifest.ManifestFiles.Files.Length; i++) { sourceFiles[i] = Path.Combine(sourcePath, manifest.ManifestFiles.Files[i].Source); targetFiles[i] = Path.Combine(path, manifest.ManifestFiles.Files[i].Source); } }
private void FileCopyWorker(Manifest manifest, string sourcePath, AsyncOperation asyncOp) { Exception exception = null; FileCopyProgressChangedEventArgs e = null; Stream rStream = null; Stream wStream = null; double writeBytes = 0; string[] sourceFiles = null; string[] targetFiles = null; GetFiles(manifest, sourcePath, out sourceFiles, out targetFiles); if (!TaskCanceled(asyncOp.UserSuppliedState)) { try { double totalBytes = GetFileLength(sourceFiles); byte[] buffer = new byte[writeFileLength]; int len = 0; int offset = 0; for (int i = 0; i < sourceFiles.Length; i++) { try { if (!Directory.Exists(Path.GetDirectoryName(targetFiles[i]))) { Directory.CreateDirectory(Path.GetDirectoryName(targetFiles[i])); } rStream = new FileStream(sourceFiles[i], FileMode.Open, FileAccess.Read, FileShare.None); wStream = new FileStream(targetFiles[i], FileMode.Create, FileAccess.Write, FileShare.None); while ((len = rStream.Read(buffer, offset, writeFileLength)) > 0) { wStream.Write(buffer, offset, len); writeBytes += len; e = new FileCopyProgressChangedEventArgs((int)(writeBytes / totalBytes * 100), asyncOp.UserSuppliedState); e.SourceFileName = sourceFiles[i]; e.TargetFileName = targetFiles[i]; e.TotalBytesToCopy = totalBytes; e.BytesToCopy = len; e.Manifest = manifest; asyncOp.Post(this.onProgressReportDelegate, e); Thread.Sleep(1); } } finally { DisposeStream(wStream); DisposeStream(rStream); } } } catch (Exception ex) { exception = ex; OnFileCopyError(new FileCopyErrorEventArgs() { Error = ex, Manifest = manifest }); } if (exception == null && targetFiles.Length > 0) { //解压文件 string fileName = targetFiles.First(); ZipHelper.DeCompressionZip(fileName, Path.GetFullPath(".")); File.Delete(fileName); } } this.CompletionMethod(manifest, exception, TaskCanceled(asyncOp.UserSuppliedState), asyncOp); }
private void CompletionMethod(Manifest manifest, Exception exception, bool canceled, AsyncOperation asyncOp) { if (!canceled) { lock (userStateToLifetime.SyncRoot) { userStateToLifetime.Remove(asyncOp.UserSuppliedState); } } FileCopyCompletedEventArgs e = new FileCopyCompletedEventArgs(exception, canceled, asyncOp.UserSuppliedState); e.Manifest = manifest; asyncOp.PostOperationCompleted(onCompletedDelegate, e); }
public void CopyAsync(Manifest manifest, string sourcePath) { CopyAsync(manifest, sourcePath, defaultTaskId); }