Install an assembly into GAC programmatically

10,255

Solution 1

You could do something like:

GacInstall((new System.IO.FileInfo("MyAssembly.dll")).FullName);

or,

GacInstall(System.IO.Path.GetFullPath("MyAssembly.dll"));

Assuming that the file is in your current working directory. If it's not, then you need to define what rules are being used to find this DLL (e.g. is it in the same path as your current executable?)

Solution 2

try this below piece I cameup with, let me know if this works

Assembly assembly = Assembly.GetAssembly(typeof(Program));//Replace your Type here.
string filePath = assembly.Location; 

Then use this file path.

Solution 3

If you know relative path of that DLL in regard of your executeble, make

string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyFullPath = Path.Combine(executableDirectory,
                            relativePathToAssembly);

Should work for you.

Share:
10,255
sharmila
Author by

sharmila

Updated on June 17, 2022

Comments

  • sharmila
    sharmila almost 2 years

    I need to install an assembly in GAC using c#. Below is my code:

    new System.EnterpriseServices.Internal.Publish().GacInstall("MyAssembly.dll");
    

    The above code gives the error:

    Absolute path required

    But i need this to run without using static file path (absolute path). Can anyone tell me whether its possible? I have added the reference to the assembly inside the project references. I need to install this assembly inside GAC.

  • Anders Marzi Tornblad
    Anders Marzi Tornblad about 10 years
    That will not work if the process has changed the current directory after starting up, or if the process was started with a different current directory than the folder of the executing assembly. The proper way would instead be to use Environment.CurrentDirectory.
  • Anders Marzi Tornblad
    Anders Marzi Tornblad about 10 years
    That would only work if the assembly is referenced from the program performing the GAC installation.
  • Tigran
    Tigran about 10 years
    @atornblad: you can set Environment.CurrentDirectory too, by changing it's path during execution and more, it's OS information, that will not in any way reflect your app setup.
  • Anders Marzi Tornblad
    Anders Marzi Tornblad about 10 years
    The question does not mention anything about only wanting to register dlls that are in the same directory as the executing app.
  • Tigran
    Tigran about 10 years
    @atornblad: and you get, paths of those assemblies.
  • WIMP_no
    WIMP_no almost 8 years
    Reviving this, sorry about that. But this isn't exactly a programmatic way of doing it.