Installer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using Shell32;
  9. using System.Reflection;
  10. using System.Diagnostics;
  11. using System.Text;
  12. using Microsoft.Win32;
  13. namespace installCompenent
  14. {
  15. [RunInstaller(true)]
  16. public partial class Installer : System.Configuration.Install.Installer
  17. {
  18. public Installer()
  19. {
  20. InitializeComponent();
  21. }
  22. /// <summary>
  23. /// 程序入口
  24. /// </summary>
  25. public static void Main()
  26. {
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. /// <param name="savedState"></param>
  32. protected override void OnCommitted(IDictionary savedState)
  33. {
  34. base.OnCommitted(savedState);
  35. }
  36. /// <summary>
  37. /// 安装程序前处理
  38. /// </summary>
  39. /// <param name="savedState"></param>
  40. protected override void OnBeforeInstall(IDictionary savedState)
  41. {
  42. KillP();//关闭程序
  43. base.OnBeforeInstall(savedState);
  44. }
  45. /// <summary>
  46. /// 安装过程
  47. /// </summary>
  48. /// <param name="stateSaver"></param>
  49. public override void Install(IDictionary stateSaver)
  50. {
  51. #region 添加到任务栏中
  52. base.Install(stateSaver);
  53. IsLock("锁定到任务栏(&K)");
  54. #endregion
  55. }
  56. /// <summary>
  57. /// 安装完成后
  58. /// </summary>
  59. /// <param name="savedState"></param>
  60. protected override void OnAfterInstall(IDictionary savedState)
  61. {
  62. base.OnAfterInstall(savedState);
  63. #region 安装完成后执行
  64. agreement();//添加自定义协议
  65. string _UPE = this.Context.Parameters["targetdir"];
  66. Directory.SetCurrentDirectory(_UPE);
  67. //Process.Start(_UPE + "UseStudio.exe", "one", null, null, "Administrators"); //要执行的程序
  68. #endregion
  69. }
  70. /// <summary>
  71. /// 卸载使用
  72. /// </summary>
  73. /// <param name="savedState"></param>
  74. protected override void OnBeforeUninstall(IDictionary savedState)
  75. {
  76. IsLock("从任务栏脱离(&K)");//脱离任务栏
  77. KillP();//关闭程序
  78. base.OnBeforeUninstall(savedState);
  79. }
  80. /// <summary>
  81. /// 卸载前的操作
  82. /// </summary>
  83. /// <param name="savedState"></param>
  84. public override void Uninstall(IDictionary savedState)
  85. {
  86. base.Uninstall(savedState);
  87. }
  88. /// <summary>
  89. /// 卸载后的操作
  90. /// </summary>
  91. /// <param name="savedState"></param>
  92. protected override void OnAfterUninstall(IDictionary savedState)
  93. {
  94. base.OnAfterUninstall(savedState);
  95. }
  96. #region 辅助函数
  97. /// <summary>
  98. /// 设置自定义协议
  99. /// </summary>
  100. public void agreement()
  101. {
  102. RegistryKey _USE = Registry.ClassesRoot.CreateSubKey("usestudio");
  103. _USE = _USE.CreateSubKey("shell"); _USE = _USE.CreateSubKey("open"); _USE = _USE.CreateSubKey("command");
  104. _USE.SetValue("", "\"" + this.Context.Parameters["targetdir"] + "usestudio.exe\"");
  105. }
  106. /// <summary>
  107. /// 是否锁定到任务栏
  108. /// </summary>
  109. /// <param name="UTF"></param>
  110. public void IsLock(params string[] USN)
  111. {
  112. try
  113. {
  114. string _UTP = this.Context.Parameters["targetdir"]; Shell _USE = new Shell();
  115. Folder _UFE = _USE.NameSpace(Path.GetDirectoryName(_UTP + "UseStudio.exe"));
  116. FolderItem _UAE = _UFE.ParseName("UseStudio.exe");
  117. foreach (FolderItemVerb _UIE in _UAE.Verbs())
  118. {
  119. if (Array.IndexOf(USN, _UIE.Name) > -1) { _UIE.DoIt(); }
  120. }
  121. }
  122. catch (Exception ex) { }
  123. }
  124. /// <summary>
  125. /// 杀死进程
  126. /// </summary>
  127. public void KillP()
  128. {
  129. Process[] processcollection = Process.GetProcessesByName("UseStudio");
  130. if (processcollection.Length >= 1)
  131. {
  132. foreach (Process process in processcollection)
  133. {
  134. if (process.ProcessName.Contains("UseStudio")) { process.Kill(); process.Close(); process.Dispose(); return; }
  135. }
  136. }
  137. }
  138. #endregion
  139. }
  140. }