GAC 安装程序
一个命令行 GAC 安装程序,即使程序集是由 MSI 安装的,也能将其卸载。
引言
Gacutil 应用程序是安装/卸载项目到/从全局程序集缓存的最常用工具。但是,它存在一些问题:
- 该应用程序未随 .NET Framework 一起分发。
- 如果程序集是由 MSI 安装程序安装的,则无法卸载该程序集。
- 如果您卸载程序集并且不知道完全限定的程序集名称(包括 PublicToken),则难以使用。
尝试卸载由 MSI 安装的程序集将生成如下错误消息:
D:\temp\aa>gacutil /u "MySDKDll, Version=1.0.0.0,
Culture=neutral, PublicKey Token=da66bde628dcedb8,
processorArchitecture=MSIL"
Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved. Assembly: MySDKDll,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=da66bde 628dcedb8,
processorArchitecture=MSIL
Unable to uninstall: assembly is required
by one or more applications Pending references:
SCHEME: <windows_installer />ID: <msi />DESCRIPTION : <windows installer="" />
Number of assemblies uninstalled = 0
Number of failures = 0
有一些工具可以完成这项工作,例如 GacView,但它不是命令行驱动的,因此不能用作安装程序的一部分(除了许可问题)。
该实用程序可以从命令行控制。即使程序集是由 MSI 安装的,它也会删除程序集。为了删除这样的程序集,必须清除注册表项。
LocalMachine\Software\Microsoft\Installer\Assemblies\Global
CurrentUser\Software\Microsoft\Installer\Assemblies\Global
该实用程序识别这些键中的条目并删除内容。
private void ClearRegKey(string AssemblyShortName, RegistryKey BaseKey)
{
RegistryKey key = BaseKey.OpenSubKey(@"Software\Microsoft\" +
@"Installer\Assemblies\Global", true);
if (key != null)
{
string[] names = key.GetValueNames();
foreach (string Name in names)
{
string[] Words = Name.Split(',');
string nn = Words[0];
string nn2 = Words[4];
if (AssemblyShortName == nn)
{
key.SetValue(Name, "", RegistryValueKind.String); key.Close();
}
return;
}
}
}
如何使用该实用程序
示例
- 安装程序集
GacInstaller.exe i "C:/Program Files/ABCD/MySDKDll.DLL"
GacInstaller.exe u MySDKDll - just the name
GacInstaller.exe u MySDKDll da66bde628dcedb8 - using name and public token
如果安装/卸载不成功,应用程序表单将变为红色,并将错误信息写入日志文件。
致谢
该实用程序使用了 Junfeng Zhang 创建的 GAC API 包装器。原始包装器代码可在 此处获得。
版本
当前版本为 1.0.0。最新版本可以从 www.dotnetremoting.com 以及其他有用的组件一起下载。