. NET Winform integrates the referenced DLL files into the EXE

Source: Internet
Author: User
Tags reflector

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 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Reflection;namespaceProxySniffer{    static classProgram    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static voidMain()        {            //用于加载引用的dll资源            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>            {                String resourceName = "ProxySniffer.lib."newAssemblyName(args.Name).Name + ".dll";                using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))                {                    Byte[] assemblyData = newByte[stream.Length];                    stream.Read(assemblyData, 0, assemblyData.Length);                    returnAssembly.Load(assemblyData);                }            };            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(newForm1());        }    }}

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.