zoukankan      html  css  js  c++  java
  • C#将dll打包到程序中

    有时候我们会使用第三方的库,好多游戏也是使用第三方库,在没有安装第三方就会出错。

    而现在我们经常使用绿色软件,希望直接运行一个软件,不要下载很多库。

    我们有一个简单的方法把dll库打包程序中

    我们可以添加AppDomain.CurrentDomain.AssemblyResolve在程序运行,发现缺少库

    如果我们把dll放到库中,属性为资源
    这里写图片描述

    我看到blqw把运行从程序找dll写为

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace blqw
    {
        /// <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
                        {
                            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
                        {
                            //加载失败就算了...
                        }
                    }
                }
            }
        }
    }

    原文:http://www.cnblogs.com/blqw/p/LoadResourceDll.html

    但是这样可能存在软件无法打开,因为没有找到已加载的同名程序集,会throw new DllNotFoundException(assName)

     //判断Dlls集合中是否有已加载的同名程序集
                if (Dlls.TryGetValue(assName, out ass) && ass != null)
                {
                    Dlls[assName] = null;//如果有则置空并返回
                    return ass;
                }
                else
                {
                    throw new DllNotFoundException(assName);//否则抛出加载失败的异常
                }

    我直接注释掉,但是这样不好,现在觉得应该使用资源文件把dll放进去,判断是否有dll,没有就从资源放到同目录,然后创建快捷方式

            private void instances(string file)
            {
                debug = file;
                Uri url = new Uri(".\" + file, UriKind.Relative);
    
                using (Stream stream = Application.GetResourceStream(url)?.Stream)
                {
                    if (stream == null)
                    {
                        return;
                    }
                    byte[] buffer = new byte[20480];
                    int n;
                    using (FileStream temp = new FileStream(file, FileMode.Create))
                    {
                        while ((n = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            temp.Write(buffer, 0, n);
                        }
                    }
                }
            }
  • 相关阅读:
    百度地图中找不到BMap的解决
    关于baidu map js api的各种踏坑
    手机版的百度map封装,使用gps定位
    js获取今天,明天,本周五,下周五日期的函数
    表格中的td内的div的文字内容禁止换行一行显示的css
    一次tomcat的调优记录
    一个input输入内容监听联动的demo
    确认框,confirm工具封装
    [Web API] Web API 2 深入系列(6) Model绑定(上)
    [Web API] Web API 2 深入系列(5) 特性路由
  • 原文地址:https://www.cnblogs.com/lindexi/p/12087596.html
Copyright © 2011-2022 走看看