簡體   English   中英

如何將DLL添加到GAC並嵌入exe

[英]How to add DLL to GAC and embed with exe

我需要C#代碼將程序集添加到GAC 有人知道如何使用C#將 DLL 添加到GAC嗎?

編輯:我正在嘗試在窗口應用程序中使用字節加載 dll。 由於某些 dll 文件在我的應用程序中正確加載,但是當我嘗試加載程序集 (Microsoft_DirectX_AudioVideoPlayback.dll) 時,它給了我 badImage 異常錯誤。 基本上我只需要使用以下方法從字節數組加載程序集。

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

其中 BytesArray 是程序集字節數組。

我收到以下行作為錯誤。

試圖加載格式不正確的程序。 (來自 HRESULT 的異常:0x8007000B)System.badImageformat 異常: {"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"}

Publish publish = new Publish();

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

命名空間:System.EnterpriseServices.Internal

程序集:System.EnterpriseServices(在 System.EnterpriseServices.dll 中)

不; 您無需向 GAC 添加任何內容。

您可以簡單地調用Assembly.Load()直接從文件中嵌入的字節數組加載程序集。
請注意,您需要在 JITer 遇到來自這些程序集中的任何類型之前執行此操作。

當您包含視頻文件時,您必須將其作為“嵌入式資源”包含在內。 如果你不知道怎么做,這里是步驟。

  1. 將您的視頻文件添加到項目中。
  2. 點擊視頻文件。 去屬性。
  3. 選擇構建操作“嵌入式資源”

然后使用下面的類將視頻作為字節數組檢索。 我相信你可以照顧其余的。

    using System.IO;
    using System.Reflection;

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

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

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

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

                    return memoryStream.ToArray();
                }
                catch
                {
                    return null;
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM