如何从C#应用程序中卸载GAC.
我无法从GAC卸载特定的exe和DLL.
这是在C#中卸载GAC的正确方法吗?
public void RemoveAssembly(string ShortAssemblyName, string PublicToken)
{
AssemblyCacheEnum AssembCache = new AssemblyCacheEnum(null);
string FullAssembName = null;
for (; ; )
{
string AssembNameLoc = AssembCache.GetNextAssembly();
if (AssembNameLoc == null)
break;
string Pt;
string ShortName = GetAssemblyShortName(AssembNameLoc, out Pt);
if (ShortAssemblyName == ShortName)
{
if (PublicToken != null)
{
PublicToken = PublicToken.Trim().ToLower();
if (Pt == null)
{
FullAssembName = AssembNameLoc;
break;
}
Pt = Pt.ToLower().Trim();
if (PublicToken == Pt)
{
FullAssembName = AssembNameLoc;
break;
}
}
else
{
FullAssembName = AssembNameLoc;
break;
}
}
}
string Stoken = "null";
if (PublicToken != null)
{
Stoken = PublicToken;
}
if (FullAssembName == null)
throw new Exception("Assembly=" + ShortAssemblyName + ",PublicToken=" +
token + " not found in GAC");
AssemblyCacheUninstallDisposition UninstDisp;
AssemblyCache.UninstallAssembly(FullAssembName, null, out UninstDisp);
}
public static void UninstallAssembly(String assemblyName, InstallReference reference, out AssemblyCacheUninstallDisposition disp)
{
AssemblyCacheUninstallDisposition dispResult = AssemblyCacheUninstallDisposition.Uninstalled;
if (reference != null)
{
if (!InstallReferenceGuid.IsValidGuidScheme(reference.GuidScheme))
throw new ArgumentException("Invalid reference guid.", "guid");
}
IAssemblyCache ac = null;
int hr = Utils.CreateAssemblyCache(out ac, 0);
if (hr >= 0)
{
hr = ac.UninstallAssembly(0, assemblyName, reference, out dispResult);
}
if (hr < 0)
{
Marshal.ThrowExceptionForHR(hr);
}
disp = dispResult;
}
Run Code Online (Sandbox Code Playgroud)
你为什么要这么做?GAC 是全局程序集缓存,基本上是包含 PC 共享 DLL 的系统文件夹。
如果您想删除程序集,请检查此处的 gacutil 文档: http: //msdn.microsoft.com/en-us/library/ex0ss12c (v=vs.71).aspx 但它将从您的所有项目中删除!
| 归档时间: |
|
| 查看次数: |
4328 次 |
| 最近记录: |