前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#项目将dll打包进exe中

C#项目将dll打包进exe中

作者头像
zls365
发布2021-04-07 10:21:50
1.1K0
发布2021-04-07 10:21:50
举报
文章被收录于专栏:CSharp编程大全CSharp编程大全

似的例子:

意图:

想将项目用到的两个dll库文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同编译进exe中,并编译后仅一个exe程序就可以独立运行不再需要其它文件。

实现:

1、将两个dll库文件作为资源文件添加进项目中;

2、添加功能代码

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.IO;
 
 
namespace AutoUpdateServer.Core
{
    /// <summary> 载入资源中的动态链接库(dll)文件
    /// </summary>
    static class LoadResourceDll
    {
        static Dictionary<string, Assembly> Dlls = new Dictionary<string, Assembly>();
        static Dictionary<string, object> Assemblies = new Dictionary<string, object>();
 
 
        static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
        {
            //程序集
            Assembly ass;
            //获取加载失败的程序集的全名
            var assName = new AssemblyName(args.Name).FullName;
            //判断Dlls集合中是否有已加载的同名程序集
            if (Dlls.TryGetValue(assName, out ass) && ass != null)
            {
                Dlls[assName] = null;//如果有则置空并返回
                return ass;
            }
            else
            {
                throw new DllNotFoundException(assName);//否则抛出加载失败的异常
            }
        }
 
 
        /// <summary> 注册资源中的dll
        /// </summary>
        public static void RegistDLL()
        {
            //获取调用者的程序集
            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();
            foreach (var r in res)
            {
                //如果是dll,则加载
                if (r.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        if (r.Contains("CryptEnDe.dll") 
                            || r.Contains("CryptEnDe_d.dll"))
                        {
                            ExtractResourceToFile(r, PathUtils.GetUpdateDllPath() + @"/" + r.Substring(r.IndexOf('.') + 1));
                        }
 
 
                        var s = ass.GetManifestResourceStream(r);
                        var bts = new byte[s.Length];
                        s.Read(bts, 0, (int)s.Length);
                        var da = Assembly.Load(bts);
                        //判断是否已经加载
                        if (Dlls.ContainsKey(da.FullName))
                        {
                            continue;
                        }
                        Dlls[da.FullName] = da;
                    }
                    catch(Exception e)
                    {
                        //加载失败就算了...
                    }
                }
            }
        }
 
 
        private static void ExtractResourceToFile(string resourceName, string filename)
        {
            //if (!System.IO.File.Exists(filename))
            {
                using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        byte[] b = new byte[s.Length];
                        s.Read(b, 0, b.Length);
                        fs.Write(b, 0, b.Length);
                    }
                }
            }
        }
 
 
    }
}

其中PathUtils.GetUpdateDllPath()函数为获取dll释放的路径,根据自己需要指定路径

代码语言:javascript
复制

public static string GetUpdateDllPath()
{
    string strPath = @"C:/LTShiyi/cache/updatedll";
    if (!Directory.Exists(strPath))
    {
       Directory.CreateDirectory(strPath);
    }
 
     return strPath;
}

3、在程序入口Program类中调用上面的接口函数

代码语言:javascript
复制

static Program()
{
   AutoUpdateServer.Core.LoadResourceDll.RegistDLL();
}

4、编译即可。

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-02,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com