C#把dll打包到exe

把其他类库生成的dll,和现在的exe打包在一起,发给别人的时候,就发一个exe即可。

一共二种方法

第一种

1.建立一个类库项目

代码

C#把dll打包到exe_第1张图片

生成dll

2.建立一个winform项目

C#把dll打包到exe_第2张图片

3.在项目中把dll引用里面去

C#把dll打包到exe_第3张图片

4.把dll直接复制到项目的根目录中

C#把dll打包到exe_第4张图片

并且修改下面2项 

C#把dll打包到exe_第5张图片

5.回到项目的界面上,在按钮中增加ClassLibrary1.dll的方法

C#把dll打包到exe_第6张图片

6.在启动的地方加上代码

C#把dll打包到exe_第7张图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    static class Program
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {

            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                //WindowsFormsApp2 这个是主程序的命名空间
                string resourceName = "WindowsFormsApp2." + new AssemblyName(args.Name).Name + ".dll";
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

7.点击生成,此时把exe发到任何电脑都是可以的。效果如下。

C#把dll打包到exe_第8张图片

 拓展1

WPF需要这样

C#把dll打包到exe_第9张图片

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// 
    /// App.xaml 的交互逻辑
    /// 
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                //WpfApp1 这个是主程序的命名空间
                string resourceName = "WpfApp1." + new AssemblyName(args.Name).Name + ".dll";
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };
        }
    }


}

拓展2

 如果大量的dll,需要建立一个文件夹,把dll都放进去 ,把dll全选设置成资源

在路径中一定要加上文件夹的名字

第二种

1.建立一个项目,再建立一个文件夹,把dll放进去

C#把dll打包到exe_第10张图片

2.对文件夹下面的dll进行设置

C#把dll打包到exe_第11张图片

 3.对引用下面的dll,复制本地改成FALSE

C#把dll打包到exe_第12张图片

 4.创建一个类

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public static class LoadResoureDll
    {
        ///  已加载DLL
        /// 
        private static Dictionary LoadedDlls = new Dictionary();
        ///  已处理程序集
        /// 
        private static Dictionary Assemblies = new Dictionary();
        ///  在对程序集解释失败时触发
        /// 
        /// AppDomain
        /// 事件参数
        private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                //程序集
                Assembly ass;
                //获取加载失败的程序集的全名
                var assName = new AssemblyName(args.Name).FullName;
                //判断Dlls集合中是否有已加载的同名程序集
                if (LoadedDlls.TryGetValue(assName, out ass) && ass != null)
                {
                    LoadedDlls[assName] = null;//如果有则置空并返回
                    return ass;
                }
                else
                {
                    return ass;//dev的dll 这里有问题,可以绕过
                    throw new DllNotFoundException(assName);//否则抛出加载失败的异常
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("error1:\n位置:AssemblyResolve()!\n描述:" + ex.Message);
                return null;
            }
        }

        ///  注册资源中的dll
        /// 
        /// *表示连续的未知字符,_表示单个未知字符,如*.dll
        public static void RegistDLL(string pattern = "*.dll")
        {
            System.IO.Directory.GetFiles("", "");
            //获取调用者的程序集
            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();
            var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
            foreach (var r in res)
            {
                //如果是dll,则加载
                if (regex.IsMatch(r))
                {
                    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 (LoadedDlls.ContainsKey(da.FullName))
                        {
                            continue;
                        }
                        LoadedDlls[da.FullName] = da;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error2:加载dll失败\n位置:RegistDLL()!\n描述:" + ex.Message);
                    }
                }
            }
        }
    }
}

5.在程序的入口处调用

C#把dll打包到exe_第13张图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    static class Program
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
            LoadResoureDll.RegistDLL();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

6.最终会生成一个独立的exe文件。

C#把dll打包到exe_第14张图片

来源:C#把dll打包到exe_c# 打包exe_故里2130的博客-CSDN博客

你可能感兴趣的:(C#,c#,visual,studio,开发语言)