Three methods, let WPF project to generate a single file

Original: three ways to make WPF project to generate a single file

When using WPF to write some small tools, often you need to embed multiple DLL files to the EXE file to generate a single file. Here are three options:

  • The DLL file as an embedded resource
  • 使用 Costura.Fody
  • Use .NET Reactor.

First, convert the file to embedded resource DLL

The first step in the new project Resourcesfolder, copy the file to the desired directory dll (dll file may be a plurality), and then modify the properties of each file, the generating operation to an embedded resource , for example:

6141703-e7b1f4709db6b2db.png
Copy dll file
6141703-38e83a734ec33361.png
Modify generating operation

The second step, modify the App.xaml.cs file, add the assembly resolution failed event and load the specified embedded resource. Modified content:


  
  
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. namespace Embed
  11. {
  12. /// <summary>
  13. /// App.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class App : Application
  16. {
  17. readonly string[] dlls = new string[] { "Newtonsoft.Json" }; // 去掉后缀名
  18. public App()
  19. {
  20. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  21. }
  22. private System.Reflection. Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  23. {
  24. string resources = null;
  25. foreach ( var item in dlls)
  26. {
  27. if (args.Name.StartsWith(item))
  28. {
  29. resources = item + ".dll";
  30. break;
  31. }
  32. }
  33. if ( string.IsNullOrEmpty(resources)) return null;
  34. var assembly = Assembly.GetExecutingAssembly();
  35. resources = assembly.GetManifestResourceNames().FirstOrDefault(s => s.EndsWith(resources));
  36. if ( string.IsNullOrEmpty(resources)) return null;
  37. using (Stream stream = assembly.GetManifestResourceStream(resources))
  38. {
  39. if (stream == null) return null;
  40. var block = new byte[stream.Length];
  41. stream.Read(block, 0, block.Length);
  42. return Assembly.Load(block);
  43. }
  44. }
  45. }
  46. }

In which dllsan array of content for the Resourcesdirectory to remove the suffix of the file name. For example, Resourcesthe directory there Newtonsoft.Json.dll, MaterialDesignThemes.Wpf.dlland MaterialDesignColors.dllthen dllsan array of content

readonly string[] dlls = new string[] { "Newtonsoft.Json" , "MaterialDesignThemes.Wpf" , "MaterialDesignColors"}; 

 
 

Finally, rebuild the project, delete generate dll files in the directory.

Second, the use Costura.Fody

Costura.Fody can be embedded as a resource reference library files, very simple to use, can be installed directly Costura.Fody:

PM> Install-Package Costura.Fody

 
 

To take a simple example:

  1. Create a new WPF project, add Newtonsoft.Json:PM> Install-Package Newtonsoft.Json
  2. Installation Costura.Fody
  3. Build the project

Generating results were as follows:

6141703-f78c18cf8fd3f3ce.png
Costura.Fody generate results
Costura.Fody link: https://github.com/Fody/Costura

Third, the use of .NET Reactor

.NET Reactor is a .NET code encryption obfuscation tool, while having a dependent scanning, and embedding assembly.
Specific Use these steps:

  1. Open the WPF project generated exe file
  2. Click Scan Dependencies button;
  3. Check embedded in all the assemblies ;
  4. Click on protection can be.
    6141703-de34781569f60b96.png
    manual
    Generating results were as follows:
    6141703-b97849d0214f74c7.png
    Generate results

In general, the above three methods can be embedded dll resources, generate a single file. Costura.Fody .NET Reactor and easy to use, minimal changes. If there is encryption requirements, it is recommended to use .NET Reactor.

Disclaimer: This article is "txfly 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https://www.jianshu.com/p/72534a7e2f4a

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12208816.html