C#中如何把dll打包到exe
把其他類庫生成的dll,和現(xiàn)在的exe打包在一起,發(fā)給別人的時(shí)候,就發(fā)一個(gè)exe即可。
一共二種方法
第一種
1.建立一個(gè)類庫項(xiàng)目
代碼
生成dll
2.建立一個(gè)winform項(xiàng)目
3.在項(xiàng)目中把dll引用里面去
4.把dll直接復(fù)制到項(xiàng)目的根目錄中
并且修改下面2項(xiàng)
5.回到項(xiàng)目的界面上
在按鈕中增加ClassLibrary1.dll的方法
6.在啟動的地方加上代碼
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 { /// <summary> /// 應(yīng)用程序的主入口點(diǎn)。 /// </summary> [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { //WindowsFormsApp2 這個(gè)是主程序的命名空間 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.點(diǎn)擊生成,此時(shí)把exe發(fā)到任何電腦都是可以的。
效果如下。
拓展1
WPF需要這樣
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 { /// <summary> /// App.xaml 的交互邏輯 /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { //WpfApp1 這個(gè)是主程序的命名空間 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,需要建立一個(gè)文件夾,把dll都放進(jìn)去 ,把dll全選設(shè)置成資源
在路徑中一定要加上文件夾的名字
第二種
1.建立一個(gè)項(xiàng)目
再建立一個(gè)文件夾,把dll放進(jìn)去
2.對文件夾下面的dll進(jìn)行設(shè)置
3.對引用下面的dll
復(fù)制本地改成FALSE
4.創(chuàng)建一個(gè)類
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 { /// <summary> 已加載DLL /// </summary> private static Dictionary<string, Assembly> LoadedDlls = new Dictionary<string, Assembly>(); /// <summary> 已處理程序集 /// </summary> private static Dictionary<string, object> Assemblies = new Dictionary<string, object>(); /// <summary> 在對程序集解釋失敗時(shí)觸發(fā) /// </summary> /// <param name="sender">AppDomain</param> /// <param name="args">事件參數(shù)</param> 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; } } /// <summary> 注冊資源中的dll /// </summary> /// <param name="pattern">*表示連續(xù)的未知字符,_表示單個(gè)未知字符,如*.dll</param> public static void RegistDLL(string pattern = "*.dll") { System.IO.Directory.GetFiles("", ""); //獲取調(diào)用者的程序集 var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly; //判斷程序集是否已經(jīng)處理 if (Assemblies.ContainsKey(ass.FullName)) { return; } //程序集加入已處理集合 Assemblies.Add(ass.FullName, null); //綁定程序集加載失敗事件(這里我測試了,就算重復(fù)綁也是沒關(guān)系的) 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); //判斷是否已經(jīng)加載 if (LoadedDlls.ContainsKey(da.FullName)) { continue; } LoadedDlls[da.FullName] = da; } catch (Exception ex) { MessageBox.Show("error2:加載dll失敗\n位置:RegistDLL()!\n描述:" + ex.Message); } } } } } }
5.在程序的入口處調(diào)用
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp5 { static class Program { /// <summary> /// 應(yīng)用程序的主入口點(diǎn)。 /// </summary> [STAThread] static void Main() { LoadResoureDll.RegistDLL(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
6.最終會生成一個(gè)獨(dú)立的exe文件
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# 鼠標(biāo)穿透窗體功能的實(shí)現(xiàn)方法
通過以下代碼,在窗體啟動后調(diào)用方法SetPenetrate() 即可實(shí)現(xiàn)窗體的穿透功能,有需要的朋友可以參考一下2013-10-10數(shù)字金額大寫轉(zhuǎn)換器制作代碼分享(人民幣大寫轉(zhuǎn)換)
一個(gè)人民幣大寫的擴(kuò)展方法,可以做成數(shù)字金額大寫轉(zhuǎn)換器,大家參考使用吧2013-12-12C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?
這篇文章主要介紹了C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開介紹,需要的朋友可以參考一下2022-08-08C#基礎(chǔ):Equals()與運(yùn)算符==的區(qū)別分析
本篇文章是對c#中的Equals()與運(yùn)算符==的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#基礎(chǔ)之Lambda表達(dá)式用法實(shí)例教程
這篇文章主要介紹了C#中Lambda表達(dá)式用法,并與之前所述的匿名方法做一比較,詳細(xì)的講述了Lambda表達(dá)式的定義及具體用法,需要的朋友可以參考下2014-09-09