Search This Blog

Saturday, July 18, 2009

C# code to register Assembly into Global Cache (GAC)

You can make use of System.EnterpriseServices assembly. Follow the following steps:

1) Add Reference >> System.EnterpriseServices
2) In case of C#:
using System.EnterpriseServices.Internal;

3) below in the code, for example on click of a button

Publish objPub=new Publish();

//to add the assembly - use full path with file name
objPub.GacInstall("AssamblyPath");

//to remove the assembly - use full path with the file name
objPub.GacRemove("AssemblyPath");

Moreove make sure, your assembly should be signed using:
sn.exe -k assemblyname //either it is .dll or .exe

Another Code Example:
using System.Text;
using System.Windows.Forms;
using System.EnterpriseServices.Internal;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Publish p = new Publish();
p.GacInstall(@"c:\myassmebly.dll");
}
}
}


Other GAC related links:

- A proper way to install Assemblies to the GAC is documented by Microsoft here http://support.microsoft.com/kb/317540/en-us

- For NSIS and .net - Register a managed .NET DLL in the GAC

No comments: