Saturday, June 22, 2019

Simple Code For (Un)Installing An Assembly To The GAC

The Microsoft tool GacUtil.exe could be in one of many places depending on what version of .Net is installed.
Rather than have a user try to find it, we can leverage the .Net Framework directly to perform GAC (un)install.
This is ideal for use in a Console application to (un)install a specific file.

Microsoft Links
Publish.GacInstall Method
Publish.GacRemove Method

Example:
Simple installer class that is installing the MyAssembly.dll assembly into the GAC.
ToDo: This may be more useful if it accepts the file name as a parameter.

using System;
using System.IO;
using System.EnterpriseServices.Internal; 
 
class GACInstaller
{
    static void Main(string[] args)
    {
        string assemblyName = "MyAssembly.dll";
        Console.WriteLine();
        Publish objPublish = new Publish();
        string file = Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
 
        if (File.Exists(file))
        {
            try
            {
                objPublish.GacInstall(file);
                Console.WriteLine($"'{assemblyName}' successfully added to the cache");
            }
            catch (Exception error)
            {
                Console.WriteLine($"Error encountered when adding '{assemblyName}' to cache: {error.Message}");
            }
        }
        else
        {
            Console.WriteLine($"Cannot find specified '{assemblyName}' in current directory");
        }
    }
}

No comments:

Post a Comment

Performance: Linking a List of Child Objects To a List of Parent Objects

Many a time I've had to build a relationship where a parent object has a list of child objects linked to it, where the parent itself als...