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();
}
///
/// 程序入口
///
public static void Main()
{
}
///
///
///
///
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
}
///
/// 安装程序前处理
///
///
protected override void OnBeforeInstall(IDictionary savedState)
{
KillP();//关闭程序
base.OnBeforeInstall(savedState);
}
///
/// 安装过程
///
///
public override void Install(IDictionary stateSaver)
{
#region 添加到任务栏中
base.Install(stateSaver);
IsLock("锁定到任务栏(&K)");
#endregion
}
///
/// 安装完成后
///
///
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
}
///
/// 卸载使用
///
///
protected override void OnBeforeUninstall(IDictionary savedState)
{
IsLock("从任务栏脱离(&K)");//脱离任务栏
KillP();//关闭程序
base.OnBeforeUninstall(savedState);
}
///
/// 卸载前的操作
///
///
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
}
///
/// 卸载后的操作
///
///
protected override void OnAfterUninstall(IDictionary savedState)
{
base.OnAfterUninstall(savedState);
}
#region 辅助函数
///
/// 设置自定义协议
///
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\"");
}
///
/// 是否锁定到任务栏
///
///
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) { }
}
///
/// 杀死进程
///
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
}
}