using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Windows.Forms;
using Shell32;
using System.Reflection;
using System.Diagnostics;
using System.Text;
using Microsoft.Win32;

namespace installCompenent
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 程序入口
        /// </summary>
        public static void Main()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnCommitted(IDictionary savedState)
        {
            base.OnCommitted(savedState);
        }

        /// <summary>
        /// 安装程序前处理
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnBeforeInstall(IDictionary savedState)
        {
            KillP();//关闭程序
            base.OnBeforeInstall(savedState);
        }

        /// <summary>
        /// 安装过程
        /// </summary>
        /// <param name="stateSaver"></param>
        public override void Install(IDictionary stateSaver)
        {
            #region 添加到任务栏中

            base.Install(stateSaver);
            IsLock("锁定到任务栏(&K)");

            #endregion
        }

        /// <summary>
        /// 安装完成后
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

            #region 安装完成后执行

            agreement();//添加自定义协议
            string _UPE = this.Context.Parameters["targetdir"];
            Directory.SetCurrentDirectory(_UPE);
            //Process.Start(_UPE + "UseStudio.exe", "one", null, null, "Administrators"); //要执行的程序 


            #endregion
        }

        /// <summary>
        /// 卸载使用
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            IsLock("从任务栏脱离(&K)");//脱离任务栏
            KillP();//关闭程序
            base.OnBeforeUninstall(savedState);
        }

        /// <summary>
        /// 卸载前的操作
        /// </summary>
        /// <param name="savedState"></param>
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }

        /// <summary>
        /// 卸载后的操作
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnAfterUninstall(IDictionary savedState)
        {
            base.OnAfterUninstall(savedState);
        }


        #region 辅助函数

        /// <summary>
        /// 设置自定义协议
        /// </summary>
        public void agreement()
        {
            RegistryKey _USE = Registry.ClassesRoot.CreateSubKey("usestudio");
            _USE = _USE.CreateSubKey("shell"); _USE = _USE.CreateSubKey("open"); _USE = _USE.CreateSubKey("command");
            _USE.SetValue("", "\"" + this.Context.Parameters["targetdir"] + "usestudio.exe\"");
        }

        /// <summary>
        /// 是否锁定到任务栏
        /// </summary>
        /// <param name="UTF"></param>
        public void IsLock(params string[] USN)
        {
            try
            {
                string _UTP = this.Context.Parameters["targetdir"]; Shell _USE = new Shell();
                Folder _UFE = _USE.NameSpace(Path.GetDirectoryName(_UTP + "UseStudio.exe"));
                FolderItem _UAE = _UFE.ParseName("UseStudio.exe");
                foreach (FolderItemVerb _UIE in _UAE.Verbs())
                {
                    if (Array.IndexOf(USN, _UIE.Name) > -1) { _UIE.DoIt(); }
                }
            }
            catch (Exception ex) { }
        }

        /// <summary>
        /// 杀死进程
        /// </summary>
        public void KillP()
        {
            Process[] processcollection = Process.GetProcessesByName("UseStudio");
            if (processcollection.Length >= 1)
            {
                foreach (Process process in processcollection)
                {
                    if (process.ProcessName.Contains("UseStudio")) { process.Kill(); process.Close(); process.Dispose(); return; }
                }
            }
        }

        #endregion
    }

}
