Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninstall from GAC In C# code

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;
}
like image 568
Rashmi A M Avatar asked Jun 20 '12 09:06

Rashmi A M


2 Answers

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

like image 127
ShaunOReilly Avatar answered Oct 12 '22 01:10

ShaunOReilly


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!

like image 39
kpull1 Avatar answered Oct 12 '22 03:10

kpull1