몇 가지 작은 도구를 작성하는 WPF를 사용하는 경우, 자주 하나의 파일을 생성하는 EXE 파일에 삽입 여러 DLL 파일을해야합니다. 여기에 세 가지 옵션은 다음과 같습니다 :
- 임베디드 리소스로 DLL 파일
- 使用 Costura.Fody
- 사용 .NET 반응기.
첫째, 임베디드 리소스 DLL에 파일을 변환
신규 프로젝트의 첫 단계 Resources
폴더에 원하는 디렉터리 DLL (DLL 파일이 복수 일 수있다)에 파일을 복사하고, 각 파일의 속성을 수정 생성 동작 에 포함 된 리소스 , 예를 들어 :
![6141703 - e7b1f4709db6b2db.png](https://ss.csdn.net/p?https://upload-images.jianshu.io/upload_images/6141703-e7b1f4709db6b2db.png)
![6141703-38e83a734ec33361.png](https://ss.csdn.net/p?https://upload-images.jianshu.io/upload_images/6141703-38e83a734ec33361.png)
두 번째 단계, 수정
App.xaml.cs
, 파일을 어셈블리 해상도 실패 이벤트를 추가하고 지정된 포함 리소스를로드합니다. 수정 된 내용 :
-
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);
-
}
-
}
-
}
-
}
하는 dlls
컨텐츠의 배열 Resources
디렉토리는 파일 이름 접미사를 제거합니다. 예를 들어, Resources
이 디렉토리 Newtonsoft.Json.dll
, MaterialDesignThemes.Wpf.dll
그리고 MaterialDesignColors.dll
다음 dlls
내용의 배열
readonly string[] dlls = new string[] { "Newtonsoft.Json" , "MaterialDesignThemes.Wpf" , "MaterialDesignColors"};
마지막으로, 디렉토리에있는 dll 파일을 생성 삭제할 프로젝트를 다시 빌드합니다.
둘째, 사용 Costura.Fody
Costura.Fody은 사용이 매우 간단하고, 자원 참조 라이브러리 파일로 포함 할 수 있습니다, 직접 Costura.Fody 설치할 수 있습니다 :
PM> Install-Package Costura.Fody
간단한 예를 들자면하려면 :
- 새로운 WPF 프로젝트를 만들고, Newtonsoft.Json를 추가 :
PM> Install-Package Newtonsoft.Json
- 설치 Costura.Fody
- 프로젝트를 빌드
다음과 같이 생성 결과는 :
![6141703 - f78c18cf8fd3f3ce.png](https://ss.csdn.net/p?https://upload-images.jianshu.io/upload_images/6141703-f78c18cf8fd3f3ce.png)
셋째, .NET 원자로의 사용
종속 주사를 갖는 조립 매립하면서 반응기 .NET은 .NET 코드 암호화 난독 도구이다.
특정 사용 다음 단계 :
- WPF 프로젝트 생성 된 exe 파일을 엽니 다
- 클릭 스캔 종속성 버튼을;
- 확인 모든 어셈블리에 포함 ;
- 을 클릭 보호 할 수 있습니다.
사용 과정결과를 생성
일반적으로 세 가지 방법 위는 DLL 자원을 포함 할 수 있습니다, 하나의 파일을 생성합니다. Costura.Fody .NET 원자로와 사용하기 쉬운, 최소한의 변경. 암호화 요구가있는 경우, .NET 반응기를 사용하는 것이 좋습니다.
면책 조항 :이 문서는 "txfly의 원래 기사, 원본 소스 링크이 문을 첨부 해주세요 재현의 CC 4.0 BY-SA 저작권 계약을 따릅니다.
원본 링크 : https://www.jianshu.com/p/72534a7e2f4a