using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Net; using System.Windows.Forms; using DevComponents.DotNetBar; using Microsoft.Win32; namespace Cyberpipe { public partial class FrmDownloadProgress : Office2007Form { string programUrl = ""; //static FrmDownloadProgress frm = null; //public static FrmDownloadProgress getForm(string url) //{ // if (frm == null) // { // frm = new FrmDownloadProgress(url); // frm.ShowDialog(); // } // return frm; //} public FrmDownloadProgress(string url) { InitializeComponent(); programUrl = url; } private void FrmDownloadProgress_FormClosing(object sender, FormClosingEventArgs e) { //frm = null; } private void FrmDownloadProgress_Load(object sender, EventArgs e) { updateVersion(programUrl); } //更新版本 static string localPath = ""; private void updateVersion(string programUrl) { try { string ent = "C:\\CyberPipe"; if (!File.Exists(ent)) { Directory.CreateDirectory(ent); } localPath = ent + "\\" + Path.GetFileName(programUrl); //TODO LIST:判断文件是否存在,存在就删除 delOldVersion(localPath); WebClient myWebClient = new WebClient(); myWebClient.DownloadProgressChanged += myWebClient_DownloadProgressChanged; myWebClient.DownloadFileCompleted += myWebClient_DownloadFileCompleted; myWebClient.DownloadFileAsync(new Uri(programUrl), localPath); } catch (Exception e) { MessageBox.Show(e.Message); } } private void createFolder(String folerName) { if (!Directory.Exists(folerName)) { Directory.CreateDirectory(folerName); } } private void delOldVersion(String fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } } void myWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { unstall(); if (File.Exists(localPath)) { Process.Start(localPath).WaitForExit(); } } //下载进度è条? void myWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { float percentage = 0f; try { progressBarX1.Maximum = (int)e.TotalBytesToReceive; progressBarX1.Value = (int)e.BytesReceived; percentage = (e.BytesReceived / (float)e.TotalBytesToReceive) * 100; } catch { throw; } double totalSize = e.TotalBytesToReceive / (1024.0 * 1024.0); double currentSize = e.BytesReceived / (1024.0 * 1024.0); labelX1.Text = "已下载" + e.ProgressPercentage + "%" + "(" + Math.Round(currentSize, 1) + "M/" + Math.Round(totalSize, 1) + "M)"; if (percentage == 100.0f) { //frm.Close(); } } private void unstall() { try { if (getProductCode("Cyberpipe") == "") { return; } String code = "/x {" + getProductCode("Cyberpipe") + "} /qr"; String sysroot = Environment.SystemDirectory; Process.Start(sysroot + "\\msiexec.exe ", code).WaitForExit(); } catch (Exception e) { MessageBox.Show("旧版本卸载失败!" + e.Message); } } private String getProductCode(String programName) { string productCode = ""; // 如果是32位操作系统,(或者系统是64位,程序也是64位) string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; RegistryKey localMachine = Registry.LocalMachine; RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true); foreach (string subkey in Uninstall.GetSubKeyNames()) { RegistryKey productcode = Uninstall.OpenSubKey(subkey); try { string displayname = productcode.GetValue("DisplayName").ToString(); if (displayname == programName) { string uninstallString = productcode.GetValue("UninstallString").ToString(); string[] strs = uninstallString.Split('{', '}'); productCode = strs[1]; return productCode; } } catch { } } return productCode; } } }