12

How do I uninstall the GAC from my C# application.

I am not able to uninstall, the particular exe and DLL from GAC.

Is it the proper way to uninstall the GAC in C# ?

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;
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
Rashmi A M
  • 242
  • 1
  • 5
  • 15
  • would you consider to unregister it by Process class? – Talha Jun 20 '12 at 10:09
  • 8
    See http://stackoverflow.com/a/2611435/17034 – Hans Passant Aug 04 '12 at 17:53
  • 3
    ["C# Source Code: Programmatically adding and removing assemblies from the GAC (without using GACUTIL)"](http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=65&PostID=1) – CiteHistory Study Aug 13 '12 at 21:03
  • You can also do this manually by navigating to C:\Windows\assembly and "deleting" the assemblies you wish to remove, if you're not actually needing programmatic removal. – TheXenocide Sep 19 '12 at 15:12
  • 1
    The link above for "C# Source Code: Programmatically adding and removing assemblies from the GAC (without using GACUTIL)" is now dead. You can find the post at archive.org--[C# Source Code: Programmatically adding and removing assemblies from the GAC (without using GACUTIL)](http://web.archive.org/web/20061015194403/http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=65&PostID=1) – Jim Jul 23 '14 at 17:56

7 Answers7

2

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...

ShaunOReilly
  • 2,186
  • 22
  • 34
1

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!

kpull1
  • 1,623
  • 15
  • 19
1

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

1

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;
}
Amadeus Sanchez
  • 2,375
  • 2
  • 25
  • 31
s.meijer
  • 3,403
  • 3
  • 26
  • 23
  • Gacutil.exe is only for development purposes and should not be used to install production assemblies into the global assembly cache. See MSDN for more http://msdn.microsoft.com/en-us/library/dkkx7f79(v=vs.110).aspx – bartonm May 09 '14 at 23:53
  • The question wasn't how to install assemblies into the GAC, but how to remove them. – s.meijer May 22 '14 at 13:43
0

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/

Jai Mallesh Babu
  • 325
  • 1
  • 3
  • 11
0

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

Enrico
  • 1,937
  • 3
  • 27
  • 40
0

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

Community
  • 1
  • 1