Newer
Older
EMS_SZ / FrmDownloadProgress.cs
root on 5 May 2016 6 KB fenye
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 = "";
        //string programConfig = "";
        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 static void updateVersion(string programUrl)
        {
            try
            {
                
                string ent = System.Environment.GetEnvironmentVariable("CyberPipe",EnvironmentVariableTarget.Machine);
                if (ent == null)
                {
                    System.Environment.SetEnvironmentVariable("CyberPipe", "C:\\CyberPipe", EnvironmentVariableTarget.Machine);
                    ent = System.Environment.GetEnvironmentVariable("CyberPipe",EnvironmentVariableTarget.Machine);
                }
                createFolder(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 static void createFolder(String folerName)
        {
            if (!Directory.Exists(folerName))
            {
                Directory.CreateDirectory(folerName);
            }
        }

        private static void delOldVersion(String fileName)
        {
          
            if (System.IO.File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        static void myWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

            unstall();
            if (File.Exists(localPath))
            {
                Process.Start(localPath).WaitForExit();
            }
            frm.Close();
           
        }

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

                }
            }
        }

        private static void unstall()
        {
            try
            {
                //  String code = getProductCode("CyberPipe");
              //  String code = "/x{b44a09f4-34f0-48f7-86eb-c5c4f526ea19}/qr";
     
                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 static 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;
        }



    }
}