C#将DLL嵌入到exe(实验可行的唯一方法)

第一:无需新增文件夹以及在此文件夹里增加DLL,因为我的VS里头Resources下面已经有了。

第二:不用把DLL的“生成操作”弄成“嵌入的资源”,节省exe空间


双击Resources.resx


选择添加现有文件,把所需DLL添加进来



这样工程下面会自动生成:



然后,不需要给DLL设置为“嵌入的资源”(反正我是不需要的)

最后,看代码:(红色字)



namespace DM
{
    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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值