using ICSharpCode.SharpZipLib.Zip; using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace AutoUpdate { public partial class UpdateForm : Form { private string _file; private string _fileName; private string _path; private CancellationTokenSource _cts = new CancellationTokenSource(); public UpdateForm(string file) { InitializeComponent(); this.StartPosition = FormStartPosition.CenterParent; this.Text += $" v{Assembly.GetExecutingAssembly().GetName().Version.ToString()}"; this._file = file; this._fileName = Path.GetFileNameWithoutExtension(file); this._path = Path.GetDirectoryName(file); } private void Form1_Load(object sender, EventArgs e) { if (MessageBox.Show("是否关闭当前运行实例进行更新?", "升级程序提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { this.button2.Enabled = true; Task.Run(() => { this.StartUpdate(); }); } else { Application.Exit(); } } private void StartUpdate() { try { var installZipFile = Path.Combine(this._path, "application.zip"); //if (File.Exists(installZipFile)) { //0.关闭进程 var instances = Process.GetProcessesByName(_fileName); this.Update(() => { this.log.Text += "开始关闭实例"; this.log.Text += Environment.NewLine; }); foreach (var item in instances) { item.Kill(); } this.Update(() => { this.log.Text += "关闭实例完成"; this.log.Text += Environment.NewLine; }); //1.备份原有内容到backup this.Update(() => { this.log.Text += "开始备份文件"; this.log.Text += Environment.NewLine; }); var folder = Path.Combine(_path, "backup"); if (Directory.Exists(folder)) { Directory.Delete(folder, true); Directory.CreateDirectory(folder); this.Update(() => { this.log.Text += "删除旧的备份文件完成"; this.log.Text += Environment.NewLine; }); } this.DirectoryCopy(this._path, folder); //2.解压升级程序到当前文件夹 this.Update(() => { this.log.Text += "开始解压新文件"; this.log.Text += Environment.NewLine; }); this.UnZip(installZipFile); //3.成功删除备份和升级文件 try { this.Update(() => { this.log.Text += "删除升级文件"; this.log.Text += Environment.NewLine; }); File.Delete(installZipFile); this.Update(() => { this.log.Text += "删除备份目录"; this.log.Text += Environment.NewLine; }); Directory.Delete(folder, true); } catch (Exception ex) { this.Update(() => { this.log.Text += ex.Message; this.log.Text += Environment.NewLine; this.log.Text += ex.StackTrace; this.log.Text += Environment.NewLine; }); } Process.Start(this._file); Application.Exit(); } } catch (Exception ex) { this.Update(() => { this.log.Text += ex.Message; this.log.Text += Environment.NewLine; this.log.Text += ex.StackTrace; this.log.Text += Environment.NewLine; }); //4.失败还原备份 //File.Delete(file); } } private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true) { if (sourceDirName == Path.Combine(this._path, "backup")) { return; } DirectoryInfo dir = new DirectoryInfo(sourceDirName); if (!dir.Exists) { throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName); } DirectoryInfo[] dirs = dir.GetDirectories(); // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); } // Get the files in the directory and copy them to the new location. FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); if (file.Name == "application.zip") { continue; } this.Update(() => { this.log.Text += temppath + "开始备份"; this.log.Text += Environment.NewLine; }); file.CopyTo(temppath, true); this.Update(() => { this.log.Text += temppath + "备份完成"; this.log.Text += Environment.NewLine; }); } // If copying subdirectories, copy them and their contents to new location. if (copySubDirs) { foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(destDirName, subdir.Name); DirectoryCopy(subdir.FullName, temppath, copySubDirs); } } } private void UnZip(string file) { using (var s = new ZipInputStream(File.OpenRead(file))) { ZipEntry theEntry = null; while ((theEntry = s.GetNextEntry()) != null) { var directoryName = Path.GetDirectoryName(theEntry.Name); var fileName = Path.GetFileName(theEntry.Name); if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { var entryName = theEntry.Name; if (entryName == "ICSharpCode.SharpZipLib.dll") { continue; } this.Update(() => { this.log.Text += entryName + "开始解压"; this.log.Text += Environment.NewLine; }); using (FileStream streamWriter = File.Create(entryName)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } this.Update(() => { this.log.Text += entryName + "解压完成"; this.log.Text += Environment.NewLine; }); } } } } private void Update(Action function) { this.Invoke(function); } } }