none
Register / unregister dll by c# code to GAC

    Question

  • Dear all

    How can i register a Dll into the GAC.

    If it Exist i want to remove it and to install again (with C#)

    in shortage

    1) Check Existance

    2) If Not Exist then Install

    3) Else Remove and ReInstall

     

    Thanks alot

     

    Sunday, April 01, 2007 12:46 PM

Answers

  • It's a security risk for any other application to directly register entries into the GAC.  Only GACUTIL is given permission to do that sort of thing, which allows administrators to ban GAC registrations through the .NET Framework adminstration tools.

    But, you could try something like this:

        internal static class GAC

        {

            /// <summary>

            /// Add strong-named assembly to GAC. DLL must be in current directory.

            /// </summary>

            /// <param name="assemblyName">name of assembly (without .dll extension).</param>

            internal void Register ( String assemblyName )

            {

                ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", string.Format("/i {0}.dll", assemblyName));

                processStartInfo.UseShellExecute = false;

                Process process = Process.Start(processStartInfo);

                process.WaitForExit;

            }

            /// <summary>

            /// Remove assembly from GAC.

            /// </summary>

            /// <param name="assemblyName">name of assembly (without .dll extension).</param>

            internal void Unregister ( String assemblyName )

            {

                ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", string.Format("/u {0}.dll", assemblyName));

                processStartInfo.UseShellExecute = false;

                Process process = Process.Start(processStartInfo);

                process.WaitForExit;

            }

        }

    Sunday, April 01, 2007 5:01 PM
    Moderator

All replies

  • It's a security risk for any other application to directly register entries into the GAC.  Only GACUTIL is given permission to do that sort of thing, which allows administrators to ban GAC registrations through the .NET Framework adminstration tools.

    But, you could try something like this:

        internal static class GAC

        {

            /// <summary>

            /// Add strong-named assembly to GAC. DLL must be in current directory.

            /// </summary>

            /// <param name="assemblyName">name of assembly (without .dll extension).</param>

            internal void Register ( String assemblyName )

            {

                ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", string.Format("/i {0}.dll", assemblyName));

                processStartInfo.UseShellExecute = false;

                Process process = Process.Start(processStartInfo);

                process.WaitForExit;

            }

            /// <summary>

            /// Remove assembly from GAC.

            /// </summary>

            /// <param name="assemblyName">name of assembly (without .dll extension).</param>

            internal void Unregister ( String assemblyName )

            {

                ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", string.Format("/u {0}.dll", assemblyName));

                processStartInfo.UseShellExecute = false;

                Process process = Process.Start(processStartInfo);

                process.WaitForExit;

            }

        }

    Sunday, April 01, 2007 5:01 PM
    Moderator
  • Ok

    So thanks for the code - i will check it up

    how do i check the existence of the dll in the GAC???

    Ron

     

     

    Monday, April 09, 2007 4:12 PM
  • Hi, the solution works fine, but it's possible to install an assembly in the GAC by code only (not by calling another process). Just wanted to mention it.

     

    Look at the Example <Program Files>\Microsoft Visual Studio .NET\FrameworkSDK\Samples\Technologies\Interop\Advanced\comreg

     

     

    Here is the test app (extracted from the sample code, i just added a Main-Function which will add/remove the executing assembly to the GAC)

     

    using System;

    using System.Text;

    using System.Reflection;

    using System.IO;

    using System.Runtime.InteropServices;

     

    public static class CMain

    {

    public static void Main()

    {

    // add

    FusionInstall.AddAssemblyToCache(Assembly.GetExecutingAssembly().Location);

    // remove

    // FusionInstall.RemoveAssemblyFromCache(assembly.GetName().Name);

    }

    }

     

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]

    internal interface IAssemblyCache

    {

    [PreserveSig()]

    int UninstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pvReserved, out uint pulDisposition);

    [PreserveSig()]

    int QueryAssemblyInfo(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pAsmInfo);

    [PreserveSig()]

    int CreateAssemblyCacheItem(uint dwFlags, IntPtr pvReserved, out /*IAssemblyCacheItem*/IntPtr ppAsmItem, [MarshalAs(UnmanagedType.LPWStr)] String pszAssemblyName);

    [PreserveSig()]

    int CreateAssemblyScavenger(out object ppAsmScavenger);

    [PreserveSig()]

    int InstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszManifestFilePath, IntPtr pvReserved);

    }

     

    public class FusionInstall

    {

    static internal int AddAssemblyToCache(string assembly)

    {

    IAssemblyCache ac = null;

    int hr = CreateAssemblyCache(out ac, 0);

    if (hr != 0)

    return hr;

    else

    return ac.InstallAssembly(0, assembly, (IntPtr)0);

    }

    static internal int RemoveAssemblyFromCache(string assembly)

    {

    IAssemblyCache ac = null;

    uint n;

    int hr = CreateAssemblyCache(out ac, 0);

    if (hr != 0)

    return hr;

    else

    return ac.UninstallAssembly(0, assembly, (IntPtr)0, out n);

    }

    [DllImport("Fusion.dll", CharSet = CharSet.Auto)]

    internal static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, uint dwReserved);

    }

    Friday, January 04, 2008 2:55 PM
  •  

    taken from Peter's code, i tried this and it worked. but still i have to manually write "[assembly:AssemblyKeyFile(“assembly.SNK")]" in the assembly. is there any way we can fully automate this process???

     

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    using System.Diagnostics;

     

    namespace RegisterAssemblyInGAC

    {

        class Program

        {

            static void Main(string[] args)

            {

                string assemblyStrongName = @"C:\ProgramiticallyAddingInGAC\ProgramiticallyAddingInGAC\bin\Debug\ProgramiticallyAddingInGAC";

                string assemblyName = @"C:\ProgramiticallyAddingInGAC\ProgramiticallyAddingInGAC\bin\Debug\ProgramiticallyAddingInGAC";

     

                ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files\Microsoft.NET\SDK\v2.0\Bin\sn.exe",string.Format(" -k {0}.snk",assemblyStrongName));

                psi.UseShellExecute = false;

                Process p = Process.Start(psi);

                p.WaitForExit();

     

    /*

    <once strong name has been given we have to add [assembly: AssemblyKeyFile(“assembly.SNK")] before we can run gacutil.exe tool. This makes it semi-automatic> any advice ???

    */

     

                ProcessStartInfo processStartInfo = new

                    ProcessStartInfo(@"C:\Program Files\Microsoft.NET\SDK\v2.0\Bin\gacutil.exe", string.Format("-i {0}.dll", assemblyName));

               

                processStartInfo.UseShellExecute = false;

                Process process = Process.Start(processStartInfo);

                process.WaitForExit();

           

            }

        }

    }

    Wednesday, April 21, 2010 3:37 PM
  • I want to register a .dll in gac.plz help me to solve out.

     

     

    what is the process to register through programming.

    Monday, March 21, 2011 9:26 AM