private void DisplayProgressThread(Object obj) { ListViewItem newLvi = (ListViewItem)obj; UpdateFile si = (UpdateFile)newLvi.Tag; long lastPos = 0; DateTime lastTime = DateTime.Now; while (si.DownloadPercent != 1) { if (si.Length > 0) { double seconds = (DateTime.Now - lastTime).TotalSeconds; if (seconds == 0.0) { continue; } long bytes = Convert.ToInt64((si.DownloadPercent * si.Length)) - lastPos; long speed = Convert.ToInt64(bytes / seconds); si.Status = String.Format("{0}/s", IoHelper.getFileLengthLevel(speed, 1)); } lastTime = DateTime.Now; lastPos = Convert.ToInt64((si.DownloadPercent * si.Length)); InvalidateListViewItem(newLvi); Thread.Sleep(1000); } si.Status = "完成"; InvalidateListViewItem(newLvi); }
private void PushNewUpdateFile(UpdateFile newFile) { ListViewItem newLvi = lvSelectUpdate.Items.Add(newFile.FileName); newLvi.SubItems.Add(newFile.FileVersion.ToString()); newLvi.SubItems.Add(IoHelper.getFileLengthLevel(newFile.Length, 2)); newLvi.Tag = newFile; newLvi.Checked = true; }
private void CheckUpdateFile(UpdateFile newFile) { FileInfo newFileInfo = new FileInfo(newFile.FullFileName); if (newFileInfo.Exists) { if (newFile.FileVersion <= GetFileVersion(newFileInfo)) { return; } } this.Invoke(new UnnamedDelegate(delegate() { PushNewUpdateFile(newFile); })); }
private void btnCanel_Click(object sender, EventArgs e) { switch (ProgramAction) { case ActionEnum.Update: { //如果不是最后一页 if (currentPage < lstPanels.Count - 2) { if (MessageBox.Show("您确定退出在线升级程序吗?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Cancel) { return; } } break; } case ActionEnum.Install: { //如果不是最后一页 if (currentPage < lstPanels.Count - 1) { if (MessageBox.Show("您确定退出在线安装程序吗?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Cancel) { return; } } else { if (cbMakeShortCutOnDesktop.Checked) { foreach (String key in dictSoftFile.Keys) { UpdateFile[] files = dictSoftFile[key]; UpdateFile mainExeFile = files[0]; String mainExeFileFullPath = GetUpdateFileFullPath(mainExeFile, key); aaaSoft.Update.Helpers.ShellHelper.CreateShortCut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), key), mainExeFileFullPath, "", Path.GetDirectoryName(mainExeFileFullPath), 1, mainExeFile.FileDescription, ""); } } } break; } } this.Close(); }
private void DownloadThread() { List <ListViewItem> lvis = new List <ListViewItem>(); this.Invoke(new UnnamedDelegate(delegate() { foreach (ListViewItem tmpLvi in lvDownloadUpdate.Items) { lvis.Add(tmpLvi); } })); foreach (ListViewItem tmpLvi in lvis) { ListViewItem newLvi = tmpLvi; UpdateFile si = (UpdateFile)newLvi.Tag; this.Invoke(new UnnamedDelegate(delegate() { newLvi.EnsureVisible(); })); si.Status = "连接中..."; //打开显示进度线程 Thread trdDisplay = new Thread(DisplayProgressThread); trdDisplay.Name = "显示进度线程"; trdDisplay.IsBackground = true; trdDisplay.Start(newLvi); si.Status = "下载中"; //开始下载 int rtnValue = DownloadUpdateFile(si); if (rtnValue != 1) { si.Status = "错误"; this.Invoke(new UnnamedDelegate(delegate() { MessageBox.Show(String.Format("下载文件 {0} 时出错,更新失败。", si.FileName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Environment.Exit(0); })); } si.Status = "完成"; } //如果是安装动作 if (ProgramAction == ActionEnum.Install) { //分发文件 foreach (String softName in dictSoftFile.Keys) { UpdateFile[] files = dictSoftFile[softName]; foreach (UpdateFile newFile in files) { String newFileFullName = GetUpdateFileFullPath(newFile, softName); IoHelper.CreateMultiFolder(Path.GetDirectoryName(newFileFullName)); if (File.Exists(newFileFullName)) { File.Delete(newFileFullName); } File.Copy(newFile.FullFileName, newFileFullName); } } //删除临时文件 foreach (UpdateFile newFile in dictDownloadFiles.Values) { File.Delete(newFile.FullFileName); } } this.Invoke(new UnnamedDelegate(delegate() { ShowPage(currentPage + 1); })); }
private void lvDownloadUpdate_DrawItem(object sender, DrawListViewItemEventArgs e) { Graphics g = e.Graphics; Rectangle AllProcessRect = e.Item.Bounds; int borderWidth = 1; AllProcessRect.X += borderWidth; AllProcessRect.Y += borderWidth; AllProcessRect.Width -= 2 * borderWidth - 1; AllProcessRect.Height -= 2 * borderWidth - 1 - 1; Rectangle BorderRect = e.Item.Bounds; BorderRect.Width -= 1; BorderRect.Height -= 1; UpdateFile si = (UpdateFile)e.Item.Tag; if (si != null) { Rectangle CurrentProcessRect = AllProcessRect; CurrentProcessRect.Width = Convert.ToInt32(AllProcessRect.Width * si.DownloadPercent); SolidBrush sb; //画边框 Color BorderColor = Color.FromArgb(185, 232, 254); if (e.Item.Selected) { sb = new SolidBrush(BorderColor); g.FillRectangle(sb, BorderRect); } //画进度 sb = new SolidBrush(Color.FromArgb(213, 251, 134)); g.FillRectangle(sb, CurrentProcessRect); //画状态 if (si.DownloadPercent == 1) { sb = new SolidBrush(Color.Green); } else { sb = new SolidBrush(Color.DarkGray); } StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Center; Rectangle tmpRect = AllProcessRect; tmpRect.Height += 2; g.DrawString(si.Status, this.Font, sb, tmpRect, sf); //画文字 SizeF statusSize = g.MeasureString(si.Status, this.Font); Rectangle TextRect = e.Item.Bounds; TextRect.Height += 2; TextRect.Width -= Convert.ToInt32(statusSize.Width); g.DrawString(e.Item.Text, this.Font, Brushes.Black, TextRect); } else { e.DrawText(); } }
//下载软件用 private String GetUpdateFileFullPath(UpdateFile newFile, String SoftwareName) { FileInfo newFileInfo = new FileInfo(String.Format("{0}/{1}/{2}/{3}", SoftwarePath, SoftwareName, newFile.FilePath, newFile.FileName)); return(newFileInfo.FullName); }
private UpdateFile[] GetSoftwareFileList(String softwareName) { List <UpdateFile> lstUpdateFiles = new List <UpdateFile>(); //得到文件信息XML字符串 String softwareInfoUrl = String.Format(GetSoftwareInfoUrl, softwareName); String softwareInfoXml = HttpHelper.GetUrlData(softwareInfoUrl, Encoding.UTF8, proxyServer, 5); if (String.IsNullOrEmpty(softwareInfoXml)) { this.Invoke(new UnnamedDelegate(delegate() { MessageBox.Show("未能从服务器获取到软件信息,程序退出!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Environment.Exit(0); })); } XmlDocument doc = new XmlDocument(); try { doc.LoadXml(softwareInfoXml); } catch { this.Invoke(new UnnamedDelegate(delegate() { MessageBox.Show("错误的软件信息格式,程序退出!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Environment.Exit(0); })); } XmlElement root = doc.DocumentElement; this.Invoke(new UnnamedDelegate(delegate() { lvSelectUpdate.Items.Clear(); lblConnectUpdateServer.Text = "获取文件列表完成,开始检测组件版本..."; pbConnectServer.Style = ProgressBarStyle.Blocks; })); //分析检测主程序文件版本 SoftwareMainProgramFile = new UpdateFile(); SoftwareMainProgramFile.FileName = root.SelectSingleNode("MainExecuteFile").InnerText; SoftwareMainProgramFile.FilePath = "/"; SoftwareMainProgramFile.FileVersion = new Version(root.SelectSingleNode("MainExecuteFileVersion").InnerText); SoftwareMainProgramFile.FileDescription = root.SelectSingleNode("Description").InnerText; SoftwareMainProgramFile.Length = Int64.Parse(root.SelectSingleNode("MainExecuteFileSize").InnerText); SoftwareMainProgramFile.DownloadUrl = String.Format(DownloadFileUrl, SoftwareMainProgramFile.FileName); SoftwareMainProgramFile.Status = "等待开始"; switch (ProgramAction) { case ActionEnum.Update: { SoftwareMainProgramFile.FullFileName = GetUpdateFileFullPath(SoftwareMainProgramFile); break; } case ActionEnum.Install: { SoftwareMainProgramFile.FullFileName = Path.Combine(SoftwarePath, SoftwareMainProgramFile.FileName); break; } } lstUpdateFiles.Add(SoftwareMainProgramFile); //分析检测组件版本 XmlNodeList subAssemblyNodes = root.SelectNodes("SubAssemblys/SubAssembly"); for (int i = 0; i <= subAssemblyNodes.Count - 1; i++) { XmlNode subAssemblyNode = subAssemblyNodes[i]; //显示分析进度 this.Invoke(new UnnamedDelegate(delegate() { pbConnectServer.Value = (i + 1) * pbConnectServer.Maximum / subAssemblyNodes.Count; })); UpdateFile newFile = new UpdateFile(); newFile.FileName = subAssemblyNode.SelectSingleNode("FileName").InnerText; newFile.FilePath = subAssemblyNode.SelectSingleNode("FilePath").InnerText; newFile.FileVersion = new Version(subAssemblyNode.SelectSingleNode("Version").InnerText); newFile.FileDescription = subAssemblyNode.SelectSingleNode("Description").InnerText; newFile.Length = Int64.Parse(subAssemblyNode.SelectSingleNode("FileSize").InnerText); switch (ProgramAction) { case ActionEnum.Update: { newFile.FullFileName = GetUpdateFileFullPath(newFile); break; } case ActionEnum.Install: { newFile.FullFileName = Path.Combine(SoftwarePath, newFile.FileName); break; } } newFile.DownloadUrl = String.Format(DownloadFileUrl, newFile.FileName); newFile.Status = "等待开始"; lstUpdateFiles.Add(newFile); } return(lstUpdateFiles.ToArray()); }
public int DownloadUpdateFile(UpdateFile si, int retryTimes) { HttpWebRequest httpReq; HttpWebResponse httpResp; List <Cookie> Cookies = new List <Cookie>(); Uri httpURL = new Uri(si.DownloadUrl); int respInfo = 0; Int32 n; httpReq = (HttpWebRequest)WebRequest.Create(httpURL); httpReq.Proxy = proxyServer; httpReq.Method = "GET"; httpReq.Timeout = 20 * 1000; httpReq.Referer = "http://www.scbeta.com"; //检查 Cookie 信息是否为空 if (Cookies.Count > 0) { httpReq.CookieContainer = new CookieContainer(); for (n = 0; n <= Cookies.Count - 1; n++) { httpReq.CookieContainer.Add(Cookies[n]); } } FileStream fStream = null; //向服务器发送请求和收取服务器回复消息 try { httpResp = (HttpWebResponse)httpReq.GetResponse(); //得到网络流 Stream revStream = httpResp.GetResponseStream(); //创建文件夹 IoHelper.CreateMultiFolder(Path.GetDirectoryName(si.FullFileName)); //创建文件 fStream = new FileStream(si.FullFileName + ".tmp", FileMode.OpenOrCreate); fStream.SetLength(0); int revCount = 0; //缓存 byte[] buffer = new byte[1024]; //当前已下载字节数 long currentCount = 0; //总字节数 long totalCount = httpResp.ContentLength; si.Length = totalCount; do { revCount = revStream.Read(buffer, 0, buffer.Length); fStream.Write(buffer, 0, revCount); currentCount += revCount; si.DownloadPercent = Convert.ToDouble(currentCount) / totalCount; } while (currentCount != totalCount); httpResp.Close(); fStream.Close(); //删除旧文件 if (System.IO.File.Exists(si.FullFileName)) { try { System.IO.File.Delete(si.FullFileName); } catch { this.Invoke(new UnnamedDelegate(delegate() { MessageBox.Show(String.Format("无法替换文件 {0} ,更新失败。", si.FileName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Environment.Exit(0); })); return(-1); } } System.IO.File.Move(si.FullFileName + ".tmp", si.FullFileName); respInfo = 1; } catch { try { fStream.Close(); System.IO.File.Delete(si.FullFileName + ".tmp"); } catch { } if (retryTimes > 0) { System.Diagnostics.Debug.Print("重试次数:" + retryTimes.ToString()); respInfo = DownloadUpdateFile(si, retryTimes - 1); } } finally { httpResp = null; } return(respInfo); }
public int DownloadUpdateFile(UpdateFile si) { return(DownloadUpdateFile(si, 5)); }