private void AsyncCallBack(IAsyncResult ar) { try { if (ar.IsCompleted) { DownloadParams dp = ar.AsyncState as DownloadParams; HttpWebResponse response = dp.Request.EndGetResponse(ar) as HttpWebResponse; Stream stream = response.GetResponseStream(); int length = (int)response.ContentLength; byte[] buffer = new byte[BufferSize]; int count = 0; using (FileStream filestream = new FileStream(dp.DestFilePath ?? "temp.zip", FileMode.Create)) { int downloaded = 0; while (true) { count = stream.Read(buffer, 0, BufferSize); downloaded += count; if (count > 0) { //notice filestream.Write(buffer, 0, count); _context.Post(d => { this.lb_progress.Text = d + "%"; }, downloaded * 100 / length); } else { break; } } } response.Close(); //unzip ZipHelper.UnZip(dp.DestFilePath, Application.StartupPath); File.Delete(dp.DestFilePath); if (!string.IsNullOrEmpty(ProcessName)) { //run controlchecker Process process = new Process(); process.StartInfo.FileName = ProcessName; process.Start(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, Url); } finally { Application.Exit(); Environment.Exit(0); } }
/// <summary> /// 解压升级文件 /// </summary> private void unzip(String zipFile, String targetFile, String password) { ICSharpCode.SharpZipLib.Core.ProgressHandler progress = new ICSharpCode.SharpZipLib.Core.ProgressHandler(UnzipProgree); ICSharpCode.SharpZipLib.Core.CompletedFileHandler complated = new ICSharpCode.SharpZipLib.Core.CompletedFileHandler(UnzipCompleted); String file = String.Format(@"{0}\{1}.zip", Application.StartupPath, zipFile); ZipHelper.UnZip(file, targetFile, password, progress, 20, complated); }
/// <summary> /// 下载安装包 /// </summary> public void DownloadInstall() { if (localversion == null || latesversion == null) { return; } if (localversion == latesversion) { nnidtext = "恭喜你,已经更新到最新版本"; MessageBox.Show(nnidtext); } else if (localversion < latesversion && File.Exists(updateDir + @"\update.xml")) { nnidtext = "发现新版本,即将下载更新补丁"; XmlDocument doc = new XmlDocument(); //加载要读取的XML doc.Load(updateDir + "\\update.xml"); //获得子节点 返回节点的集合 XmlNode Update = doc.SelectSingleNode("update"); XmlNode version = Update.SelectSingleNode("version"); XmlNode upPath = version.SelectSingleNode("upPath"); XmlNode upname = version.SelectSingleNode("upname"); XmlNode upfileSize = version.SelectSingleNode("upfileSize"); zipname = upname.InnerText; zipUrl = serverURL + upPath.InnerText; if (File.Exists(updateDir + @"\" + upname.InnerText)) { //MessageBox.Show(updateDir + @"/" + upname.InnerText+" "+ route); ZipHelper.UnZip(updateDir + @"/" + upname.InnerText, route); File.Delete(updateDir + @"\update.xml"); File.Delete(updateDir + @"/" + upname.InnerText); MessageBoxButtons messButton = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("升级成功,确认重启软件?", "提示", messButton); if (dr == DialogResult.OK) { Process.Start(route + @"/Warehouse.exe"); Application.Exit(); } else { Application.Exit(); } } //else if (!File.Exists(updateDir + @"\" + upname.InnerText)) //{ // //如果一次没有下载成功,则检查三次 // for (int i = 1; i < 3; i++) // { // client.DownloadFile(upPath.InnerText, updateDir + @"\" + upname); // } // nnidtext = "下载失败,请检查您的网络连接是否正常"; // Environment.Exit(0); //} } }