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
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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()); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?