c#的.net工程(winform桌面窗体应用)如何将引用的dll嵌入到exe中
https://linxinfa.blog.csdn.net/article/details/109187375
注意以下几点:1.需要引用的dll直接复制加入到自己的工程中,属性生成操作:嵌入的资源
2.正常添加dll的引用,设置复制本地属性给false;
3.注册AppDomain.CurrentDomain.AssemblyResolve事件,在CLR调用程序解析失败的时候会触发AssemblyResolve事件;
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { ////// 应用程序的主入口点。 /// [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { //项目的命名空间为winform1, 嵌入dll资源在libs文件夹下,所以这里用的命名空间为: winform1.libs. string dllName = "WindowsFormsApplication1." + new AssemblyName(args.Name).Name + ".dll"; using (var dllStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(dllName)) { byte[] dllData = new byte[dllStream.Length]; dllStream.Read(dllData, 0, dllData.Length); return Assembly.Load(dllData); } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show(InNeverClass.CommonClass.GetStr("hello world")); } } }