How to add DLL to GAC and embed with exe


I need the C# code to add the assemblies to the GAC. Does anybody know how to add DLLs to GAC using C#?

EDIT: I am trying to load a dll using bytes in a window application. As some of the dll file loads properly in my application but when I am trying to load the assembly (Microsoft_DirectX_AudioVideoPlayback.dll) it is giving me error of badImage exception. Basically I just need to load the assembly from bytes array using the following method.

byte[] ByteArray = Resource1.Microsoft_DirectX_AudioVideoPlayback;
 Assembly.Load(ByteArray );

where BytesArray is the assembly bytes array.

I am getting following lines as an error.

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) System.badImageformat exception :{"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"}

asked on Stack Overflow May 30, 2013 by Vijay Singh Rana • edited Mar 14, 2021 by Vijay Singh Rana

3 Answers

Publish publish = new Publish();


Namespace: System.EnterpriseServices.Internal

Assembly: System.EnterpriseServices (in System.EnterpriseServices.dll)

answered on Stack Overflow May 30, 2013 by MikroDel • edited May 30, 2013 by MikroDel

No; you don't need to add anything to the GAC.

You can simply call Assembly.Load() to load the assemblies directly from the byte arrays embedded in your file.
Note that you will need to do that before the JITer encounters any types from those assemblies.

answered on Stack Overflow May 30, 2013 by SLaks

When you include your video file you have to include it as a "Embedded Resource". If you do not know how to do it here are the steps.

  1. Add your video file to the project.
  2. Click on the video file. Go to properties.
  3. Select build action "Embedded Resource"

Then use below class to retrieve the video as a byte array. I am sure you can take care of the rest.

    using System.IO;
    using System.Reflection;

    namespace MyProject.Video
        class MyVideoClass
            private const string videoExtract = "MyProject.Video.MyVideo.dat";      

            public byte[] GetStream()
                    var memoryStream = new MemoryStream();
                    Stream sourceStream = null;

                    sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(videoExtract); 

                    if (sourceStream != null) sourceStream.CopyTo(memoryStream);

                    return memoryStream.ToArray();
                    return null;
answered on Stack Overflow Jun 6, 2013 by Kosala W

User contributions licensed under CC BY-SA 3.0