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();


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

1 在資源中添加資源

  項目名稱----->右鍵------>屬性 ----->資源---->文件----添加資源>---->選中要添加的dll-->添加dll
2 此時項目中自動生成Resources文件夾,選擇添加的dll文件,在屬性中將 生成操作 設置為 “嵌入的資源”

3 項目---->右鍵----->添加引用----->瀏覽------>選中Resources文件夾中的dll文件------>屬性 ----->復制本地---設置為 false

4 右鍵項目,添加類,名稱為LoadResourceDll.cs

內容在后面

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

新添加的類: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());
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM