using PCRobot.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace PCRobot { public partial class WechatUpdate : Form { private string version; private string downPath = string.Empty; public WechatUpdate(string version) { InitializeComponent(); this.version = version; if (versions.ContainsKey(version)) downPath = versions[version]; } //private Dictionary versions = new Dictionary() { { "2.8.0.112", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_2.8.0.112_A.zip" }, { "2.7.1.88", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_2.7.1.88_A.zip" },{ "2.7.1.82", "http://qiniu.down.api.52cmg.cn/PCWechat_2.7.1.82.zip" },{ "3.0.0.1001", "https://dldir1.qq.com/wework/work_weixin/WXWork_3.0.0.1001.exe" },{"2.8.0.121", "http://qiniu.down.api.52cmg.cn/PCWechat_2.8.0.121.zip" } }; private Dictionary versions = new Dictionary() { { "2.9.5.41", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_2.9.5.41.zip" }, { "2.8.0.112", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_2.8.0.112_A.zip" }, { "2.7.1.88", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_2.7.1.88_A.zip" }, { "2.7.1.82", "http://qiniu.down.api.52cmg.cn/PCWechat_2.7.1.82.zip" }, { "3.0.0.1001", "http://qiniu.down.api.52cmg.cn/WXWork3.0.0.1001.zip" }, { "3.0.14.1205", "http://qiniu.down.api.52cmg.cn/WXWork3.0.14.1205_.zip" }, { "3.0.27.2701", "http://qiniu.down.api.52cmg.cn/WXWork3.0.27.2701.zip" }, { "3.1.1.3001", "http://qiniu.down.api.52cmg.cn/WXWork3.1.1.3001.zip" }, { "2.8.0.121", "http://qiniu.down.api.52cmg.cn/PCWechat_2.8.0.121.zip" }, { "3.3.0.93", "http://qiniu.down.api.52cmg.cn/PCWechat3.3.0.93.zip" }, { "3.1.10.3010", "http://qiniu.down.api.52cmg.cn/WXWork3.1.10.3010.zip" }, { "3.2.1.154", "http://qiniu.down.api.52cmg.cn/PCWechatUpdate_3.2.1.154.zip" }, { "4.0.2.6026", "http://qiniu.down.api.52cmg.cn/WXWork4.0.2.6026.zip" },{ "3.4.0.38", "http://qiniu.down.api.52cmg.cn/PCWechat3.4.0.38.zip" }, { "3.3.5.50", "http://qiniu.down.api.52cmg.cn/PCWechat3.3.5.50.zip" } }; private static Stream st = null; private static Stream so = null; private bool IsClose = false; public bool IsOk = false; private void DownloadFile(string URL, string filename, ProgressBar prog) { float percent = 0; try { if (File.Exists(filename)) File.Delete(filename); System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; prog.Invoke(new Action(delegate { prog.Maximum = (int)totalBytes; })); st = myrp.GetResponseStream(); so = new System.IO.FileStream(filename, System.IO.FileMode.Create); try { long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { if (IsClose) return; totalDownloadedByte = osize + totalDownloadedByte; Application.DoEvents(); so.Write(by, 0, osize); if (IsClose) return; prog.Invoke(new Action(delegate { prog.Value = (int)totalDownloadedByte; })); osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } } catch (Exception ex) { if (so != null) { so.Close(); so.Dispose(); so = null; } if (st != null) { st.Close(); st.Dispose(); st = null; } throw ex; } finally { if (so != null) { so.Close(); so.Dispose(); so = null; } if (st != null) { st.Close(); st.Dispose(); st = null; } } } catch (Exception ex) { throw ex; } } private void ChangeStatus(string text, params object[] objs) { try { if (!this.IsDisposed && !this.IsClose) { var temp = string.Format(text, objs); this.label1.Invoke(new Action(delegate { this.label1.Text = temp; })); } } catch (Exception ex) { } } private Thread thread; // private bool is_ing; private void button2_Click(object sender, EventArgs e) { button2.Enabled = false; try { if (!string.IsNullOrWhiteSpace(downPath)) { thread = new Thread(new ThreadStart(delegate { try { var name = DateTime.Now.Ticks + ".zip"; var filename = CsharpHttpHelper.HttpExtend.MapFile(name, "Cache\\Update"); ChangeStatus($"正在下载微信({version})版本..."); DownloadFile(downPath, filename, this.progressBar1); //if (version == "3.0.0.1001") //{ // var newname = filename.Replace(name, name.Replace(".zip", ".exe")); // File.Move(filename, newname); // OnlineUpdate.Client.StartProcess(newname); // ChangeStatus($"下载完成,为您打开了安装界面,请安装!"); // MessageBox.Show("下载完成,点击确定后,将弹出安装界面!","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Information); // //MessageBox.Show("下载完成"); // //MessageBox.Show(""); //} //else { ChangeStatus($"正在安装微信({version})版本..."); ZipArchive.UnZip(filename, System.IO.Directory.GetCurrentDirectory()); ChangeStatus("更新完成!"); IsOk = true; } if (!IsClose && !this.IsDisposed) { IsClose = true; this.Invoke(new Action(delegate () { //MessageBox.Show($"微信({version})版本安装已完成,请重新登陆微信!","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.Close(); })); } } catch (Exception ex) { if (!IsClose) MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (!IsClose && !this.IsDisposed) { this.Invoke(new Action(delegate () { button2.Enabled = true; })); } } })); thread.IsBackground = true; thread.Start(); } else throw new Exception("版本信息异常,无法获取下单地址"); } catch (Exception ex) { ChangeStatus("更新失败:" + ex.Message); if (!IsClose) MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { //button2.Enabled = true; } } private void WechatUpdate_FormClosing(object sender, FormClosingEventArgs e) { if (thread != null && thread.IsAlive && !IsClose) { var rst = MessageBox.Show($"安装微信({version})尚未完成,您确定要终止安装吗?", "友情提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (rst == DialogResult.Yes) { IsClose = true; try { thread.Abort(); } catch (Exception) { } } else e.Cancel = true; } } private void WechatUpdate_Load(object sender, EventArgs e) { } } }