2012-06-20 46 views
12

如何从我的C#应用​​程序中卸载GAC。从GAC卸载在C#代码中

我无法从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; 
} 
+0

您会考虑通过Process类取消注册吗? – Talha

+8

参见http://stackoverflow.com/a/2611435/17034 –

+3

[ “C#源代码:以编程从GAC添加和移除组件(不使用GACUTIL)”](http://www.vbusers.com/codecsharp/ codeget.asp?ThreadID = 65&PostID = 1) –

回答

1

按照此步骤: -

using System; 
using System.Reflection; 
private string assemblyFullPath = ""; 
public class myAssembly 
{ 
static void Main() 
{ 
Assembly assembly = Assembly.Load("library, version=1.0.0.0, culture=neutral, PublicKeyToken=9b184fc90fb9648d"); 
assemblyFullPath = assembly.Location; 

} 
} 

您也可以加载LoadFrom大会类的()方法汇编文件,但它并不好使用它,你可以阅读后于此知道原因。

一旦我们有了assemblyFullPath,很容易从GAC中卸载/移除程序集。 System.EnterpriseServices.Internal命名空间提供了安装或删除GAC文件的方法。按照以下步骤来安装/删除文件:

  1. 在您的项目中添加对System.EnterpriseServices.dll的引用。
  2. 使用System.EnterpriseServices.Internal添加;在使用部分的末尾。
  3. 现在使用assemblyFullPath,在要删除程序集文件的方法中添加以下代码行。

    发布publishProcess = new Publish(); publishProcess.GacRemove(assemblyFullPath);

希望这将有助于你

0

为什么不直接从应用程序中调用“gacutil/u”?或者我错过了什么?

1

建议您不要这样做,因为它可能会导致使用该DLL的其他应用程序出现问题。但我认为这是你自己的,所以它是相对安全的。

使用下一个代码从您的GAC中删除该dll。 asssembly名称是没有扩展名的dll的名称。

public static void UninstallAssembly(string assemblyName) { 
    ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", 
     string.Format("/u {0}.dll", assemblyName)); 

    processStartInfo.UseShellExecute = false; 
    Process process = Process.Start(processStartInfo); 
    process.WaitForExit; 
} 
+0

Gacutil.exe仅用于开发目的,不应用于将生产装配件安装到全局程序集缓存中。请参阅MSDN更多http://msdn.microsoft.com/en-us/library/dkkx7f79(v=vs.110).aspx – bartonm

+0

的问题并不是如何组件安装到GAC,但如何将其删除。 –

2

假设DLL是在同一目录中,其中该代码运行,尝试:

ArrayList libraries = new ArrayList(); 
libraries.Add("somedll1.dll"); 
libraries.Add("somedll2.dll"); 

Console.WriteLine("Registering DDLs in the Gac"); 

try 
{ 
    for (int i = 0; i < libraries.Count; i++) 
    { 
     // get the path from the running exe, and remove the running exe's name from it 
     new System.EnterpriseServices.Internal.Publish().GacRemove(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("ThisUtilitiesName.exe", "") + libraries[i]); 
    } 

    Console.WriteLine("Completed task successfully"); 
} 
catch (Exception exp) 
{ 
    Console.WriteLine(exp.Message); 
} 

希望这可以帮助别人......

0

你可以看到.NET参考代码详细介绍如何以编程方式完成(虽然参考站点的许可证将限制您在自己的软件中使用它,即复制粘贴此代码不是一个好主意)。

http://referencesource.microsoft.com/#System.Web/xsp/system/Web/Configuration/GacUtil.cs

(向下滚动到公共BOOL GacUnInstall(字符串的AssemblyName))

又见IAssemblyCache

http://referencesource.microsoft.com/#System.Web/xsp/system/Web/Configuration/IAssemblyCache.cs

和GetAssemblyCache

http://referencesource.microsoft.com/#System.Web/xsp/system/Web/NativeMethods.cs

本机方法

在另一方面,由于该代码是在组件的System.Web和类被标记为内部,可以使用反射来访问它。请参阅

How to access internal class using Reflection