using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using System.Diagnostics; using System.IO; namespace Cyberpipe { public partial class FrmDownloadProgress : DevComponents.DotNetBar.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); System.Net.WebClient myWebClient = new System.Net.WebClient(); myWebClient.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(myWebClient_DownloadProgressChanged); myWebClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(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 (System.IO.File.Exists(fileName)) { File.Delete(fileName); } } void myWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { unstall(); if (File.Exists(localPath)) { Process.Start(localPath).WaitForExit(); } } //下载进度è条? void myWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) { float percentage = 0f; try { progressBarX1.Maximum = (int)e.TotalBytesToReceive; progressBarX1.Value = (int)e.BytesReceived; percentage = ((float)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 = System.Environment.SystemDirectory; System.Diagnostics.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"; Microsoft.Win32.RegistryKey localMachine = Microsoft.Win32.Registry.LocalMachine; Microsoft.Win32.RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true); foreach (string subkey in Uninstall.GetSubKeyNames()) { Microsoft.Win32.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(new char[2] { '{', '}' }); productCode = strs[1]; return productCode; } } catch { continue; } } return productCode; } } }