第一:无需新增文件夹以及在此文件夹里增加DLL,因为我的VS里头Resources下面已经有了。
第二:不用把DLL的“生成操作”弄成“嵌入的资源”,节省exe空间
双击Resources.resx
选择添加现有文件,把所需DLL添加进来
这样工程下面会自动生成:
然后,不需要给DLL设置为“嵌入的资源”(反正我是不需要的)
最后,看代码:(红色字)
{
public partial class MainForm : Form
{
public MainForm()
{
//添加程序集解析事件
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
InitializeComponent();
}
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
byte[] bytes = (byte[])rm.GetObject(dllName);
return System.Reflection.Assembly.Load(bytes);
}
}
}
原文转自:https://i-blog.csdnimg.cn/blog_migrate/28e598abc4ee32519a649966fc374000.png