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

建立Installer安装类,如图:

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

代码如下:

 using Microsoft.Win32;
using ResourceShare.UserClient.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text; namespace HtmlExtractor
{
[RunInstaller(true)]
public partial class InstallerIEPlugin : System.Configuration.Install.Installer
{ private static string STRING_INTERNET_EXPLORER_MENU_EXT_NAME = "右键菜单名称(可修改)"; private static string ClassId = "DCA4FF81-B81E-4ADF-B6A8-886DEEFE3250"; public InstallerIEPlugin()
{
InitializeComponent();
} public static bool Install(string dllFullName)
{
bool result = false;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
StringBuilder builder = new StringBuilder();
Win32APIsFull.GetWindowsDirectory(builder, ); processStartInfo.FileName = Path.Combine(builder.ToString(), "Microsoft.NET" + Path.DirectorySeparatorChar + "Framework" + Path.DirectorySeparatorChar + string.Format("v{0}", Environment.Version.ToString()) + Path.DirectorySeparatorChar + "RegAsm.exe");
processStartInfo.Arguments = string.Format("\"{0}\" /codebase", dllFullName);
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(processStartInfo).WaitForExit(); try
{
//为mscrollpath重新建立路径
using (RegistryKey clessids = Registry.ClassesRoot.OpenSubKey("CLSID\\{" + ClassId + "}\\InprocServer32", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (clessids != null)
{
clessids.SetValue("", Path.Combine(Environment.SystemDirectory, "mscoree.dll"));
}
}
//删除所有ShareyIE右键
using (RegistryKey menuExtKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
foreach (string subName in menuExtKey.GetSubKeyNames())
{
if (subName.Contains("Sharey"))
{
menuExtKey.DeleteSubKey(subName);
}
}
}
result = true;
}
catch (Exception ex)
{
result = false;
Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
} try
{
using (RegistryKey internetExplorerMenuExt = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
internetExplorerMenuExt.SetValue("", Path.Combine(Path.GetDirectoryName(dllFullName), @"IEExtractor.htm"));
internetExplorerMenuExt.SetValue("Contexts", , RegistryValueKind.DWord);
}
result = true;
}
catch (Exception ex)
{
result = false;
Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
}
return result;
} public static bool UnInstall(string dllFullName)
{
bool result = false;
try
{
Registry.CurrentUser.DeleteSubKey(@"Software\Microsoft\Internet Explorer\MenuExt\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, false); using (RegistryKey menuExtKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
foreach (string subName in menuExtKey.GetSubKeyNames())
{
if (subName.Contains("Sharey"))
{
menuExtKey.DeleteSubKey(subName);
}
}
}
result = true;
}
catch (Exception ex)
{
result = false;
Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
} try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(); StringBuilder builder = new StringBuilder();
Win32APIsFull.GetWindowsDirectory(builder, ); processStartInfo.FileName = Path.Combine(builder.ToString(), "Microsoft.NET" + Path.DirectorySeparatorChar + "Framework" + Path.DirectorySeparatorChar + string.Format("v{0}", Environment.Version.ToString()) + Path.DirectorySeparatorChar + "RegAsm.exe"); processStartInfo.Arguments = string.Format(" \"{0}\" /unregister", dllFullName);
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(processStartInfo).WaitForExit();
result = true;
}
catch (Exception ex)
{
result = false;
Logger.Debug(ex.Message + "\r\n" + ex.StackTrace);
}
return result;
} public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
} public override void Commit(IDictionary savedState)
{
Install(this.Context.Parameters["assemblypath"]); base.Commit(savedState);
} public override void Rollback(IDictionary savedState)
{
UnInstall(this.Context.Parameters["assemblypath"]); base.Rollback(savedState);
} public override void Uninstall(IDictionary savedState)
{
UnInstall(this.Context.Parameters["assemblypath"]); base.Uninstall(savedState);
} public static bool IsSetuped()
{
return (Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\MenuExt\\" + STRING_INTERNET_EXPLORER_MENU_EXT_NAME, RegistryKeyPermissionCheck.ReadSubTree) != null);
}
}
}

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

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


下一篇:很好很实用的.net、网站系统后台模板