Share via


How to Install Assemblies to the GAC Programmatically?

You can add or remove assemblies from the GAC using code. You can use Publish.GacInstall method to install an assembly to the GAC, or Publish.GacRemove method to remove that assembly from the GAC.

If you are using Vista, and enable UAC, you have to run this code using elevated command (run as Administrator), otherwise it won't work.

The other thing that I discovered, this code won't install assembly that is not signed, unfortunately, I did not see any error message/exception. So check if the assembly is a strong-name assembly before installing it.

To check if an assembly is signed:

 System.Reflection.Assembly.LoadFile("My.Cool.Assembly.dll").GetName().GetPublicKey().Length > 0;

To install an assembly into the GAC:

 new System.EnterpriseServices.Internal.Publish().GacInstall("My.Cool.Assembly.dll");

To uninstall an assembly from the GAC:

 new System.EnterpriseServices.Internal.Publish().GacRemove("My.Cool.Assembly.dll");