1. 程式人生 > >C#將專案的dll,打包到exe檔案中,使用LoadResourceDll類

C#將專案的dll,打包到exe檔案中,使用LoadResourceDll類

使用這種方法,可以使工程生成的exe檔案直接執行,不需要將dll儲存在同一個檔案中,複製exe檔案直接可以拷貝使用

使用方法:

分為兩種情況:

 1 工程已載入好dll,正在使用dll

2工程新建,需要使用一些dll, dll還沒有開始使用

第一種情況:專案已經載入好,生成exe檔案,但是單獨的exe檔案還不可以使用,需要將dll檔案載入到資源中,使exe檔案可以單獨使用

1 在資源中新增資源

  專案名稱----->右鍵------>屬性 ----->資源---->檔案----新增資源>---->選中要新增的dll-->新增dll


2 此時專案中自動生成Resources資料夾,(但是這些dll不能新增到專案中,因為專案中dll已經存在,已經引用)

3 在debug中有本來生成的dll檔案,選中dll檔案,在屬性中將生成操作改為“嵌入的資源”

4 右鍵專案,新增類,名稱為LoadResourceDll.cs

內容在後面

5 在Program.cs中的Main()方法中前面加入LoadResoureDll.RegistDLL();

over

第二種方法:在專案開發中,直接將dll作為嵌入資源使用

1 在資源中新增資源

  專案名稱----->右鍵------>屬性 ----->資源---->檔案----新增資源>---->選中要新增的dll-->新增dll

2 此時專案中自動生成Resources資料夾,選擇新增的dll檔案,在屬性中將 生成操作 設定為 “嵌入的資源”

3 專案---->右鍵----->新增引用----->瀏覽------>選中Resources資料夾中的dll檔案------>屬性 ----->複製本地---設定為 false

4 右鍵專案,新增類,名稱為LoadResourceDll.cs

內容在後面

5 在Program.cs中的Main()方法中前面加入LoadResoureDll.RegistDLL();

over


新新增的類:LoadResourceDll.cs

內容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace LegendTool
{
    public static class LoadResoureDll
    {
        /// <summary> 已載入DLL
        /// </summary>
        private static Dictionary<string, Assembly> LoadedDlls = new Dictionary<string, Assembly>();
        /// <summary> 已處理程式集
        /// </summary>
        private static Dictionary<string, object> Assemblies = new Dictionary<string, object>();
        /// <summary> 在對程式集解釋失敗時觸發
        /// </summary>
        /// <param name="sender">AppDomain</param>
        /// <param name="args">事件引數</param>
        private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                //程式集
                Assembly ass;
                //獲取載入失敗的程式集的全名
                var assName = new AssemblyName(args.Name).FullName;
                //判斷Dlls集合中是否有已載入的同名程式集
                if (LoadedDlls.TryGetValue(assName, out ass) && ass != null)
                {
                    LoadedDlls[assName] = null;//如果有則置空並返回
                    return ass;
                }
                else
                {
                    throw new DllNotFoundException(assName);//否則丟擲載入失敗的異常
                }
            }
            catch (System.Exception ex)
            {
                return null;
                MessageBox.Show("error:\n位置:AssemblyResolve()!\n描述:" + ex.Message);
            }
        }

        /// <summary> 註冊資源中的dll
        /// </summary>
        /// <param name="pattern">*表示連續的未知字元,_表示單個未知字元,如*.dll</param>
        public static void RegistDLL(string pattern = "*.dll")
        {
            System.IO.Directory.GetFiles("", "");
            //獲取呼叫者的程式集
            var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
            //判斷程式集是否已經處理
            if (Assemblies.ContainsKey(ass.FullName))
            {
                return;
            }
            //程式集加入已處理集合
            Assemblies.Add(ass.FullName, null);
            //繫結程式集載入失敗事件(這裡我測試了,就算重複綁也是沒關係的)
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
            //獲取所有資原始檔檔名
            var res = ass.GetManifestResourceNames();
            var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
            foreach (var r in res)
            {
                //如果是dll,則載入
                if (regex.IsMatch(r))
                {
                    try
                    {
                        var s = ass.GetManifestResourceStream(r);
                        var bts = new byte[s.Length];
                        s.Read(bts, 0, (int)s.Length);
                        var da = Assembly.Load(bts);
                        //判斷是否已經載入
                        if (LoadedDlls.ContainsKey(da.FullName))
                        {
                            continue;
                        }
                        LoadedDlls[da.FullName] = da;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error:載入dll失敗\n位置:RegistDLL()!\n描述:" + ex.Message);
                    }
                }
            }
        }
    }
}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Tool
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            LoadResourceDll.RegistDLL();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}