首页 > 其他 > 详细

将DLL嵌入EXE

时间:2020-06-29 14:32:25      阅读:43      评论:0      收藏:0      [点我收藏+]

First, add the dll to Reference and set Copy Local to False.

技术分享图片

Then create a new project folder "libs", copy the referenced dll into this folder, and set Build Action to "Embedded Resources"

技术分享图片

In the main() method, add code to handle the exception "dll not found".

    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 e)
    {
        //embeddll是项目的命名空间, dll资源在libs文件夹下,所以这里用的命名空间为: embeddll.libs.
        string _resName = "embeddll.libs." + new AssemblyName(e.Name).Name + ".dll";
        using (var _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(_resName))
        {
            byte[] _data = new byte[_stream.Length];
            _stream.Read(_data, 0, _data.Length);
            return Assembly.Load(_data);
        }
    }

将DLL嵌入EXE

原文:https://www.cnblogs.com/jizhiqiliao/p/13207660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!