Newer
Older
GHFX_REFACTOR / UpDateProgress / UpDateProgress / FrmDownloadProgress.cs
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;
using Microsoft.Win32;

namespace UpDateProgress
{
    public partial class FrmDownloadProgress : Form
    {
        string programUrl = "";

        public FrmDownloadProgress(string url)
        {
            InitializeComponent();
            programUrl = url;
        }

        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);
                
                //localPath = System.Environment.GetEnvironmentVariable("temp") + "\\" + Path.GetFileName(programUrl);

                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();
            }
            this.Close();
        }
        /// <summary>
        /// 启动安装的程序Cyberpipe
        /// </summary>
        void StartCyberpipe()
        {

            RegistryKey appPath = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Cyberpipe.exe");
            try
            {
                string s1 = appPath.GetValue("path").ToString();//将path的值转型为String                
                Process.Start(s1);//运行安装目录重的WinWOrd程序,启动完成,s1是文件夹,WINWORD.exe是文件夹下的程序,如果s1是exe的绝对路径的话,则可以省略后面的+"程序名称"

                MessageBox.Show("Success!");

            }
            catch//异常抛出,如果没有找到该注册表值,执行下面的代码
            {
                MessageBox.Show(this, "您没有安装规划分析软件,注册表中没有Cyberpipe!");
            }

        }

        //下载进度è条?
         void myWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
         {
             //if (frm != null)
             //{
             float percentage = 0f;
             try
             {
                 progressBar1.Maximum = (int)e.TotalBytesToReceive;
                 progressBar1.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);
             label2.Text = "已下载" + e.ProgressPercentage + "%" + "(" + Math.Round(currentSize, 1) + "M/" + Math.Round(totalSize, 1) + "M)";
             if (percentage == 100.0f)
             {
                 //this.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;
        }

    }
}