using ICSharpCode.SharpZipLib.Zip; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WDProduceToolUpdate { public partial class frmProduceUpdate : Form { public frmProduceUpdate() { InitializeComponent(); } WdProduceToolVersion version = null; string updateFileName = string.Empty; private void update_Load(object sender, EventArgs e) { updateprocess(); } private void updateprocess() { try { string versionStr = JsonFileHelper.GetJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json"); if (string.IsNullOrEmpty(versionStr)) { string message = string.Format("获取更新文件失败!"); if (DialogResult.OK == MessageBox.Show(message, "系统错误", MessageBoxButtons.OK)) { Application.Exit(); } } if (!Directory.Exists(Application.StartupPath + @"update")) { Directory.CreateDirectory(Application.StartupPath + @"update"); } version = JsonConvert.DeserializeObject(versionStr); string[] filePaths = version.update_file_path.Split(@"/"); updateFileName= filePaths[filePaths.Length - 1]; WebClient wc = new WebClient(); wc.DownloadProgressChanged += wc_DownloadProgressChanged; wc.DownloadFileAsync(new Uri(version.server_url + @"/" + version.update_file_path), Application.StartupPath + @"update\"+ updateFileName); } catch (Exception er) { label1.Text = "下载失败:" + er.Message; } } private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { Action act = () => { this.progressBar1.Value = e.ProgressPercentage; this.label1.Text = "正在下载..."; //this.label1.Text = e.ProgressPercentage + "%"; }; this.Invoke(act); if (e.ProgressPercentage == 100) { //下载完成之后开始覆盖 this.label1.Text = "正在解压..."; try { var result = UnZip(Application.StartupPath + @"update\"+ updateFileName, Application.StartupPath,""); //var result = unZip(Application.StartupPath + "\\update.rar", @"..\LWH\"); if (result) { progressBar1.Value = 100; this.label1.Text = "准备安装..."; this.label1.Text = "更新完成"; string versionStr = JsonFileHelper.GetJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json"); version = JsonConvert.DeserializeObject(versionStr); version.updated = true; JsonFileHelper.WriteJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json", JsonConvert.SerializeObject(version)); var mainFile = Application.StartupPath + "WDProduceTool.exe";//重新启动软件 Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = mainFile; p.StartInfo.CreateNoWindow = true; p.Start(); this.Close(); } else { MessageBox.Show("更新失败:" + result); this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "更新失败"); this.Close(); } } } /// /// ZIP:解压一个zip文件 /// add yuangang by 2016-06-13 /// /// 需要解压的Zip文件(绝对路径) /// 解压到的目录 /// 解压密码 /// 是否覆盖已存在的文件 public bool UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true) { //如果解压到的目录不存在,则报错 if (!System.IO.Directory.Exists(TargetDirectory)) { throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!"); } //目录结尾 if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; } using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile))) { zipfiles.Password = Password; ZipEntry theEntry; while ((theEntry = zipfiles.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(TargetDirectory + directoryName); if (fileName != "") { if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName))) { using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = zipfiles.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, size); else break; } streamWriter.Close(); } } } } zipfiles.Close(); return true; } } } }