WinForm programs often need to refer to some third-party DLL files, these DLLs are published with the EXE file in the same directory, although the integration of DLL files into the EXE will increase the file size, but the program directory is relatively neat.
Here is a relatively simple integration approach and considerations.
The first step is to copy the DLL files that you need to reference into your project
You can: 1. Copy to the project root directory; 2. Copy to a folder.
Here is an example of copying a DLL to the "Lib" folder.
Copy the DLL file you want to refer to in the Lib folder
The second step is to refer to the DLL files copied to the project
Right-click on "References" or the project name and select "Add Reference (F) ..."
Switch to the Browse tab in the Add Reference dialog box and select the DLL file that you just copied from the project directory.
The third step is to set the property of the reference item to "Copy to output directory" to "Do not copy"
Right-click on the item in the References folder that you just finished adding the reference to and click Properties
Change the Copy local entry in the property pages to False.
Fourth, set the DLL file properties previously copied to the project
Right-click the DLL file that you started copying to the project directory and click Properties.
Set the copy to output directory in the Properties page to no copy, and build action to embedded resource.
At this point, all the file and reference settings are over, and here's the final key step:
Fifth step, add the load file code in the Program.cs
Since the file is integrated into the EXE, it will not be loaded automatically, so you need to tell the program here to load the DLL files that are integrated in the EXE when the load fails.
Add the following highlighted code in the Program.cs, in order to fail the Assembly resolution (because we set not to copy the DLL to the root directory, so the parsing failure here is naturally not find the referenced DLL file), tell the program to load the DLLs that have been integrated into the EXE.
It is important to note that you set the DLL file prefix ([resourcename] variable in the sample code below): Files that are integrated into the project are automatically added to the "assembly name" and "folder name" before the original file name. See the resource file in the EXE file opened with the Reflector tool below, like this: "[assembly name]." [Folder name]. [Original file name] ".
If the DLL file is copied to the root directory, then the integrated file name is: "[assembly name]. [Original file name] ".
If the DLL file is copied to the DLL directory, then the integrated file name is: "[assembly name].dll. [Original file name] ".
123456789101112131415161718192021222324252627282930313233 |
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Windows.Forms;
using
System.Reflection;
namespace
ProxySniffer
{
static class
Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void
Main()
{
//用于加载引用的dll资源
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
String resourceName =
"ProxySniffer.lib."
+
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());
}
}
}
|
Open EXE file with reflector to see the internal resources at a glance, this you can confirm the summary of the law:
The assembly name can also be obtained using the following methods:
1 |
Assembly.GetExecutingAssembly().GetName().Name |
Finally, re-publish the program, EXE becomes larger, the same directory of DLL files also disappeared.
. NET Winform integrates the referenced DLL files into the EXE (GO)