原文:将dll放进exe[.Net]
两种方案:
1、使用ILMerge工具。
缺点:需离开工程,使用第三方工具(ILMerge)。
2、将dll作为Resource放进exe,exe执行时动态加载(Load)Resources内的dll。
缺点:需编写多余代码,加载速度问题。
参考代码:
public partial class App : Application { public App() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { Assembly thisAssembly = Assembly.GetExecutingAssembly(); //Get the Name of the AssemblyFile var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll"; //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name)); if (resources.Count() > 0) { var resourceName = resources.First(); using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName)) { if (stream == null) return null; var block = new byte[stream.Length]; stream.Read(block, 0, block.Length); return Assembly.Load(block); } } return null; }; } }
时间: 2024-11-01 14:59:03