/// <summary> /// 讲更新文件下载到本地临时文件夹(未使用) /// </summary> /// <param name="info"></param> private bool Update(VersionInfo info) { if (info.UpdateFileList.Count == 0) { return(false); } string path = Path.GetTempPath(); long downSize = 0; long totalSize = info.TotalFileSize; tempFold = LocalFilesOperation.CreateNewVersionFold(path, info.UpdateTime); AutoUpdateEvent(this, new AutoUpdateEventArgs("开始更新文件", 0, 0, ProgressBarStateEnum.Continue)); //更新压缩包单文件代码如下 if (info.UpdateFileList.Count == 1 && info.UpdateFileList[0].FileName.EndsWith(".zip")) { VersionFileInfo c = info.UpdateFileList[0]; string filename = Path.GetFileName(c.FileName); downSize += c.FileSize; //AutoUpdateEvent(this, new AutoUpdateEventArgs("下载文件" + filename+"...", totalSize, downSize, ProgressBarStateEnum.Continue)); string tempFilename = Path.Combine(tempFold, filename); AutoUpdateEvent(this, new AutoUpdateEventArgs("解压缩中...", 0, 0, ProgressBarStateEnum.Marquee)); if (!dataAccesser.SaveUpdateFile(tempFold, c)) { AutoUpdateEvent(this, new AutoUpdateEventArgs(string.Format("文件{0}更新失败", filename), 0, 0, ProgressBarStateEnum.UnEnable)); return(false); } AutoUpdateEvent(this, new AutoUpdateEventArgs("文件" + filename + "更新完成", 0, 0, ProgressBarStateEnum.UnEnable)); return(true); } foreach (VersionFileInfo c in info.UpdateFileList) { if (IsCancel) { return(false); } if (c.FileOperateType != OperateType.Del) { string filename = Path.GetFileName(c.FileName); downSize += c.FileSize; AutoUpdateEvent(this, new AutoUpdateEventArgs("开始更新文件...", 0, 0, ProgressBarStateEnum.Continue)); // filename = Path.Combine(info.UpdateTime.ToString(PathAndFolder.TimeFoldFormat), filename); //string Downfilename = Path.Combine(info.UpdateTime.ToString(LocalFilesOperation.TimeFoldFormat), filename); //GetFile(tempFold, Downfilename, c.FileSize); string tempFilename = Path.Combine(tempFold, filename); if (!dataAccesser.SaveUpdateFile(tempFold, c)) { AutoUpdateEvent(this, new AutoUpdateEventArgs(string.Format("文件{0}更新失败", filename), 0, 0, ProgressBarStateEnum.UnEnable)); return(false); } AutoUpdateEvent(this, new AutoUpdateEventArgs("文件" + filename + "更新完成", 0, 0, ProgressBarStateEnum.UnEnable)); } } return(true); }
private static void CreateFold(string fullFileName) { if (!File.Exists(fullFileName)) { string directoryname = LocalFilesOperation.GetParentDirectory(fullFileName); if (!Directory.Exists(directoryname)) { Directory.CreateDirectory(directoryname); } } }
private void InstallFile(string localFileName, VersionFileInfo fileInfo, string tempFold) { string path = this.mainProcessInfo.UpdateApplicationStartupFolder; string newFileName; if (fileInfo.RelativePath != null) { newFileName = LocalFilesOperation.PathCombine(path, fileInfo.RelativePath); } else { newFileName = LocalFilesOperation.PathCombine(path, localFileName); } if (File.Exists(newFileName)) { BackUpFile(newFileName); File.Copy(newFileName, Path.Combine(backupFold, localFileName), true); } localFileName = Path.Combine(tempFold, localFileName); switch (fileInfo.FileOperateType) { case OperateType.Add: CreateFold(newFileName); File.Copy(localFileName, newFileName, true); break; case OperateType.Update: CreateFold(newFileName); File.Copy(localFileName, newFileName, true); break; case OperateType.Del: if (File.Exists(newFileName)) { File.Delete(newFileName); } break; case OperateType.Run: File.Copy(localFileName, newFileName, true); string dir = path; RunFile(dir, fileInfo.FileName); break; } }
private bool DownLoadZip(VersionInfo info) { AutoUpdateEvent(this, new AutoUpdateEventArgs("准备更新" + "...", (long)(info.TotalFileSize * 1.1), 1, ProgressBarStateEnum.Continue)); tempFold = LocalFilesOperation.CreateNewVersionFold(Path.GetTempPath(), DateTime.Now); string tempFilename = Path.Combine(tempFold, "update.zip"); HttpHelper.DonwloadSizeAction += DownloasSizeCallback; HttpHelper.Download(info.HttpGetInfo.updateUrl, tempFilename); //AutoUpdateEvent(this, new AutoUpdateEventArgs("解压缩中...", (long)(info.TotalFileSize * 1.25), (long)(info.TotalFileSize * 1.25), ProgressBarStateEnum.Continue)); if (!ZipFileOperation.UnZipFile(tempFilename, tempFold)) { AutoUpdateEvent(this, new AutoUpdateEventArgs(string.Format("文件下载异常,更新失败"), 0, 0, ProgressBarStateEnum.UnEnable)); return(false); } AutoUpdateEvent(this, new AutoUpdateEventArgs("文件下载完成", 0, 0, ProgressBarStateEnum.UnEnable)); return(true); }
/// <summary> /// 将要更新的文件保存在临时文件夹中 /// </summary> /// <param name="tempFolder"></param> /// <param name="fileInfo"></param> public bool SaveUpdateFile(string tempFolder, VersionFileInfo fileInfo) { string tempFileName = LocalFilesOperation.PathCombine(tempFolder, fileInfo.FileName); HttpHelper.Download(fileInfo.DownloadUrl, tempFileName); //压缩包 if (fileInfo.FileName.EndsWith(".zip")) { string err; if (!ZipFileOperation.UnZipFile(tempFileName, tempFolder, out err)) { return(false); } //string versionFilePath = LocalFilesOperation.PathCombine(tempFolder, "VersionInfo.txt"); //if (!File.Exists(versionFilePath)) // return false; //using (FileStream fs = new FileStream(versionFilePath, FileMode.Open, FileAccess.Read)) //{ // using (StreamReader sr = new StreamReader(fs)) // { // VersionInfos.TotalFileSize = long.Parse(sr.ReadLine()); // VersionInfos.UpdateFileList.Clear(); // List<string> infoList = new List<string>(); // while (!sr.EndOfStream) // { // infoList.Add(sr.ReadLine()); // } // foreach (string item in infoList) // { // string[] paramList = item.Split(new char[] { ',' }); // VersionInfos.UpdateFileList.Add(new VersionFileInfo() { FileName = paramList[0], RelativePath = paramList[1], FileOperateType = (OperateType)Enum.Parse(typeof(OperateType), paramList[2]), FileSize = long.Parse(paramList[3]) }); // } // } //} } else if (fileInfo.FileName.EndsWith(".exe")) { fileInfo.FileOperateType = OperateType.Run; } return(true); }
public bool SaveUpdateFile(string tempFolder, VersionFileInfo fileInfo) { string tempFileName = LocalFilesOperation.PathCombine(tempFolder, fileInfo.FileName); File.Copy(@"E:\" + fileInfo.FileName, tempFileName, true); string err; if (!ZipFileOperation.UnZipFile(tempFileName, tempFolder, out err)) { return(false); } string versionFilePath = LocalFilesOperation.PathCombine(tempFolder, "VersionInfo.txt"); if (!File.Exists(versionFilePath)) { return(false); } using (FileStream fs = new FileStream(versionFilePath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { VersionInfos.TotalFileSize = long.Parse(sr.ReadLine()); VersionInfos.UpdateFileList.Clear(); List <string> infoList = new List <string>(); while (!sr.EndOfStream) { infoList.Add(sr.ReadLine()); } foreach (string item in infoList) { string[] paramList = item.Split(new char[] { ',' }); VersionInfos.UpdateFileList.Add(new VersionFileInfo() { FileName = paramList[0], RelativePath = paramList[1], FileOperateType = (OperateType)Enum.Parse(typeof(OperateType), paramList[2]), FileSize = long.Parse(paramList[3]) }); } } } return(false); }
/// <summary> /// 本地更新文件覆盖安装目录 /// </summary> /// <param name="info"></param> private void Install(VersionInfo info) { AutoUpdateEvent(this, new AutoUpdateEventArgs("开始安装文件", (long)(info.TotalFileSize * 1.1), info.TotalFileSize, ProgressBarStateEnum.Continue)); backupFold = LocalFilesOperation.CreateNewVersionFold(this.mainProcessInfo.UpdateApplicationStartupFolder, info.UpdateTime); long totalSize = info.TotalFileSize; long downSize = 0; double cupple = info.TotalFileSize * 0.1 / info.UpdateFileList.Count; int countIndex = 1; try { foreach (VersionFileInfo c in info.UpdateFileList) { if (IsCancel) { return; } string filename = Path.GetFileName(c.FileName); downSize += c.FileSize; AutoUpdateEvent(this, new AutoUpdateEventArgs("正在安装文件" + filename + "...", (long)(info.TotalFileSize * 1.1), info.TotalFileSize + (long)cupple * countIndex, ProgressBarStateEnum.Continue)); System.Threading.Thread.Sleep(100); countIndex++; InstallFile(filename, c, tempFold); } AutoUpdateEvent(this, new AutoUpdateEventArgs("安装完成", 0, 0, ProgressBarStateEnum.UnEnable)); } catch (Exception e) { Log.LogException(e); AutoUpdateEvent(this, new AutoUpdateEventArgs("安装失败!", 0, 0, ProgressBarStateEnum.UnEnable)); RollBack(); } LocalFilesOperation.DelFold(tempFold); if (backupFold != null) { LocalFilesOperation.DelFold(backupFold); } }
private AutoUpdater() { dataAccesser = new HttpDataAccess(); //获取主程序信息 RegistryKey key = Registry.LocalMachine; RegistryKey temp; if (Environment.Is64BitOperatingSystem) { temp = key.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Mofanghr"); } else { temp = key.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Mofanghr"); } string tempName = ConfigSetting.GetConfigValue("UpdateApplicationEntryAssembly"); Console.WriteLine(tempName); mainProcessInfo.UpdateApplicationEntryAssembly = temp.GetValue(tempName).ToString(); mainProcessInfo.UpdateApplicationStartupFolder = LocalFilesOperation.GetParentDirectory(mainProcessInfo.UpdateApplicationEntryAssembly); string exeName = Path.GetFileName(mainProcessInfo.UpdateApplicationEntryAssembly); mainProcessInfo.MainProcessName = exeName.Remove(exeName.LastIndexOf(".exe")); }