c#项目将dll打包到exe中的步骤是什么

  介绍

这篇文章将为大家详细讲解有关c#项目将dll打包到exe中的步骤是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

意图:

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

实现:

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

2,添加功能代码

using 系统;   using  System.Collections.Generic;   using  System.Diagnostics;   using  System.Reflection;   using 先;   ,   ,   namespace  AutoUpdateServer.Core   {///才能,& lt; summary>,载入资源中的动态链接库(dll)文件///才能,& lt;/summary>   static 才能;class  LoadResourceDll   {才能   ,,,static  Dictionary<字符串,Assembly>, Dlls =, new  Dictionary<字符串,Assembly> ();   ,,,static  Dictionary<字符串,object>, Assemblies =, new  Dictionary<字符串,object> ();   ,   ,   ,,,static  Assembly  AssemblyResolve (object ,发送方,ResolveEventArgs  args)   ,,,{   ,,,,,//程序集   ,,,,,Assembly 驴;   ,,,,,//获取加载失败的程序集的全名   ,,,,,var  assName =, new  AssemblyName (args.Name) .FullName;   ,,,,,//判断dll集合中是否有已加载的同名程序集   ,,,,,if  (Dlls.TryGetValue (assName, out 屁股),,,,ass  !=, null)   ,,,,,{   ,,,,,,,dll (assName),=,零;//如果有则置空并返回   ,,,,,,,return 驴;   ,,,,,}   ,,,,,   ,,,,,{   ,,,,,,,throw  new  DllNotFoundException (assName);//否则抛出加载失败的异常   ,,,,,}   ,,,}   ,   ,   ,,,///,& lt; summary>,注册资源中的dll   ,,,///,& lt;/summary>   ,,,public  static  void  RegistDLL ()   ,,,{   ,,,,,//获取调用者的程序集   ,,,,,var  ass =, new 加亮(0).GetFrame (1) .GetMethod () .Module.Assembly;   ,,,,,//判断程序集是否已经处理   ,,,,,if  (Assemblies.ContainsKey (ass.FullName))   ,,,,,{   ,,,,,,,返回;   ,,,,,}   ,,,,,//程序集加入已处理集合   ,,,,,Assemblies.Add (ass.FullName, null);   ,,,,,//绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)   ,,,,,AppDomain.CurrentDomain.AssemblyResolve  +=, AssemblyResolve;   ,,,,,//获取所有资源文件文件名   ,,,,,var  res =, ass.GetManifestResourceNames ();   ,,,,,foreach  (var  r  res拷贝)   ,,,,,{   ,,,,,,,//如果是dll,则加载   ,,,,,,,if  (r.EndsWith (“.dll",, StringComparison.OrdinalIgnoreCase))   ,,,,,,,{   ,,,,,,,,,试一试   ,,,,,,,,,{   ,,,,,,,,,,,if  (r.Contains (“CryptEnDe.dll"),   ,,,,,,,,,,,,,| |,r.Contains (“CryptEnDe_d.dll"))   ,,,,,,,,,,,{   ,,,,,,,,,,,,,ExtractResourceToFile (r, PathUtils.GetUpdateDllPath (), +, @"/? +, r.Substring (r.IndexOf(& # 39; # 39;公司),+,- 1));   ,,,,,,,,,,,}   ,   ,   ,,,,,,,,,,,var  s =, ass.GetManifestResourceStream (r);   ,,,,,,,,,,,var  bts =, new 字节(s.Length);   ,,,,,,,,,,,s.Read (bts, 0, (int) s.Length);   ,,,,,,,,,,,var  da =, Assembly.Load (bts);   ,,,,,,,,,,,//判断是否已经加载   ,,,,,,,,,,,if  (Dlls.ContainsKey (da.FullName))   ,,,,,,,,,,,{   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

c#项目将dll打包到exe中的步骤是什么