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 Resources
folder, 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:
The second step, modify the
App.xaml.cs
file, add the assembly resolution failed event and load the specified embedded resource. Modified content:
-
using System;
-
using System.Collections.Generic;
-
using System.Configuration;
-
using System.Data;
-
using System.IO;
-
using System.Linq;
-
using System.Reflection;
-
using System.Threading.Tasks;
-
using System.Windows;
-
-
namespace
Embed
-
{
-
/// <summary>
-
/// App.xaml 的交互逻辑
-
/// </summary>
-
public
partial
class
App :
Application
-
{
-
readonly
string[] dlls =
new
string[] {
"Newtonsoft.Json" };
// 去掉后缀名
-
public App()
-
{
-
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
-
}
-
-
private System.Reflection.
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
-
{
-
string resources =
null;
-
foreach (
var item
in dlls)
-
{
-
if (args.Name.StartsWith(item))
-
{
-
resources = item +
".dll";
-
break;
-
}
-
}
-
if (
string.IsNullOrEmpty(resources))
return
null;
-
-
var assembly = Assembly.GetExecutingAssembly();
-
resources = assembly.GetManifestResourceNames().FirstOrDefault(s => s.EndsWith(resources));
-
-
if (
string.IsNullOrEmpty(resources))
return
null;
-
-
using (Stream stream = assembly.GetManifestResourceStream(resources))
-
{
-
if (stream ==
null)
return
null;
-
var block =
new
byte[stream.Length];
-
stream.Read(block,
0, block.Length);
-
return Assembly.Load(block);
-
}
-
}
-
}
-
}
In which dlls
an array of content for the Resources
directory to remove the suffix of the file name. For example, Resources
the directory there Newtonsoft.Json.dll
, MaterialDesignThemes.Wpf.dll
and MaterialDesignColors.dll
then dlls
an 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:
- Create a new WPF project, add Newtonsoft.Json:
PM> Install-Package Newtonsoft.Json
- Installation Costura.Fody
- Build the project
Generating results were as follows:
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:
- Open the WPF project generated exe file
- Click Scan Dependencies button;
- Check embedded in all the assemblies ;
- Click on protection can be.
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