IE浏览器右键菜单插件开发(下篇)——如何用c#安装、卸载IE右键插件

建立Installer安装类,如图:

 

代码如下:

  1 using Microsoft.Win32;
  2 using ResourceShare.UserClient.Common;
  3 using System;
  4 using System.Collections;
  5 using System.Collections.Generic;
  6 using System.ComponentModel;
  7 using System.Configuration.Install;
  8 using System.Diagnostics;
  9 using System.IO;
 10 using System.Linq;
 11 using System.Text;
 12 
 13 namespace HtmlExtractor
 14 {
 15     [RunInstaller(true)]
 16     public partial class InstallerIEPlugin : System.Configuration.Install.Installer
 17     {
 18 
 19         private static string STRING_INTERNET_EXPLORER_MENU_EXT_NAME = "右键菜单名称(可修改)";
 20 
 21         private static string ClassId = "DCA4FF81-B81E-4ADF-B6A8-886DEEFE3250";
 22 
 23         public InstallerIEPlugin()
 24         {
 25             InitializeComponent();
 26         }
 27 
 28         public static bool Install(string dllFullName)
 29         {
 30             bool result = false;
 31             ProcessStartInfo processStartInfo = new ProcessStartInfo();
 32             StringBuilder builder = new StringBuilder();
 33             Win32APIsFull.GetWindowsDirectory(builder, 255);
 34 
 35             processStartInfo.FileName = Path.Combine(builder.ToString(), "Microsoft.NET" + Path.DirectorySeparatorChar + "Framework" + Path.DirectorySeparatorChar + string.Format("v{0}", Environment.Version.ToString(3)) + Path.DirectorySeparatorChar + "RegAsm.exe");
 36             processStartInfo.Arguments = string.Format("\"{0}\" /codebase", dllFullName);
 37             processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 38             Process.Start(processStartInfo).WaitForExit();
 39 
 40             try
 41             {
 42                 //为mscrollpath重新建立路径
 43                 using (RegistryKey clessids = Registry.ClassesRoot.OpenSubKey("CLSID\\{" + ClassId + "}\\InprocServer32", RegistryKeyPermissionCheck.ReadWriteSubTree))
 44                 {
 45                     if (clessids != null)
 46                     {
 47                         clessids.SetValue("", Path.Combine(Environment.SystemDirectory, "mscoree.dll"));
 48                     }
 49                 }
 50                 //删除所有ShareyIE右键
 51                 using (RegistryKey menuExtKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt", RegistryKeyPermissionCheck.ReadWriteSubTree))
 52                 {
 53                     foreach (string subName in menuExtKey.GetSubKeyNames())
 54                     {
 55                         if (subName.Contains("Sharey"))
 56                         {
 57                             menuExtKey.DeleteSubKey(subName);
 58                         }
 59                     }
 60                 }
 61                 result = true;
 62             }
 63             catch (Exception ex)
 64             {
 65                 result = false;
 66                 Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
 67             }
 68 
 69             try
 70             {
 71                 using (RegistryKey internetExplorerMenuExt = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, RegistryKeyPermissionCheck.ReadWriteSubTree))
 72                 {
 73                     internetExplorerMenuExt.SetValue("", Path.Combine(Path.GetDirectoryName(dllFullName), @"IEExtractor.htm"));
 74                     internetExplorerMenuExt.SetValue("Contexts", 243, RegistryValueKind.DWord);
 75                 }
 76                 result = true;
 77             }
 78             catch (Exception ex)
 79             {
 80                 result = false;
 81                 Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
 82             }
 83             return result;
 84         }
 85 
 86         public static bool UnInstall(string dllFullName)
 87         {
 88             bool result = false;
 89             try
 90             {
 91                 Registry.CurrentUser.DeleteSubKey(@"Software\Microsoft\Internet Explorer\MenuExt\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, false);
 92 
 93                 using (RegistryKey menuExtKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt", RegistryKeyPermissionCheck.ReadWriteSubTree))
 94                 {
 95                     foreach (string subName in menuExtKey.GetSubKeyNames())
 96                     {
 97                         if (subName.Contains("Sharey"))
 98                         {
 99                             menuExtKey.DeleteSubKey(subName);
100                         }
101                     }
102                 }
103                 result = true;
104             }
105             catch (Exception ex)
106             {
107                 result = false;
108                 Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
109             }
110 
111             try
112             {
113                 ProcessStartInfo processStartInfo = new ProcessStartInfo();
114 
115                 StringBuilder builder = new StringBuilder();
116                 Win32APIsFull.GetWindowsDirectory(builder, 255);
117 
118                 processStartInfo.FileName = Path.Combine(builder.ToString(), "Microsoft.NET" + Path.DirectorySeparatorChar + "Framework" + Path.DirectorySeparatorChar + string.Format("v{0}", Environment.Version.ToString(3)) + Path.DirectorySeparatorChar + "RegAsm.exe");
119 
120                 processStartInfo.Arguments = string.Format(" \"{0}\" /unregister", dllFullName);
121                 processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
122 
123                 Process.Start(processStartInfo).WaitForExit();
124                 result = true;
125             }
126             catch (Exception ex)
127             {
128                 result = false;
129                 Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
130             }
131             return result;
132         }
133 
134         public override void Install(IDictionary stateSaver)
135         {
136             base.Install(stateSaver);
137         }
138 
139         public override void Commit(IDictionary savedState)
140         {
141             Install(this.Context.Parameters["assemblypath"]);
142 
143             base.Commit(savedState);
144         }
145 
146         public override void Rollback(IDictionary savedState)
147         {
148             UnInstall(this.Context.Parameters["assemblypath"]);
149 
150             base.Rollback(savedState);
151         }
152 
153         public override void Uninstall(IDictionary savedState)
154         {
155             UnInstall(this.Context.Parameters["assemblypath"]);
156 
157             base.Uninstall(savedState);
158         }
159 
160         public static bool IsSetuped()
161         {
162             return (Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\MenuExt\\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, RegistryKeyPermissionCheck.ReadSubTree) != null);
163         }
164     }
165 }

第33行,调用了win32:的方法:

        /// <summary>
        /// 获取系统安装目录
        /// </summary>
        /// <param name="SysDir"></param>
        /// <param name="count"></param>
        [DllImport("kernel32.dll")]
        public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);

 

posted @ 2017-12-12 15:16  micDavid  阅读(650)  评论(0编辑  收藏  举报