Uninstall from GAC In C# code

夙愿已清 提交于 2019-12-04 00:31:28

Assuming the DLLs are in the same directory as where this code is running from, try:

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);
}

Hope this helps someone...

Why you want to do that? GAC is the Global Assembly Cache, basicly is the system folder that contains your PC's Shared DLLs.

If you want to remove the assembly anyway check the gacutil documentation here: http://msdn.microsoft.com/en-us/library/ex0ss12c(v=vs.71).aspx But it will be removed for all your projects!

Follow this steps :-

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;

} 
} 

You can also load the assembly file with LoadFrom() method of Assembly class but it is not good to use it, you can read the post hereto know the reasons.

Once we have the assemblyFullPath, it is easy to uninstall/ remove the assembly from GAC. The System.EnterpriseServices.Internal namespace provides methods to install or remove GAC files. Follow the below steps to do install/remove the files:

  1. Add reference to System.EnterpriseServices.dll in your project.
  2. Add using System.EnterpriseServices.Internal; at the end of the using's section.
  3. Now with the assemblyFullPath, add the below lines of code in your method where you want to remove the assembly files.

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

Hope It will help you

s.meijer

You're advised to not do it, as it can cause problems to other applications using that dll. But I assume it's your own, so it is relatively safe.

Use the next code to remove the dll from your GAC. The asssembly name is the name of your dll without the extension.

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;
}

How to install an assembly into the Global Assembly Cache in Visual C#:

Here is the solution from Microsoft support article: http://support.microsoft.com/kb/815808

Another link(registering, unregistering libraries from GAC): http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7d3165cf-ca3b-43cc-8f77-a46dbf38f13d/

Why don't you just call "gacutil /u " from the application? Or am I missing something?

Eusebio Rufian-Zilbermann

You can see the .NET reference code for details on how it can done programmatically (although the license for the reference site will restrict you from using it in your own software, i.e., copy-pasting this code is not a good idea).

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

(scroll down to public bool GacUnInstall(string assemblyName))

See also IAssemblyCache

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

and the native methods for GetAssemblyCache

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

On the other hand, since the code is in assembly System.Web and the class is marked as internal, you can use reflection to access it. See

How to access internal class using Reflection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!