C # Single File application

Source: Internet
Author: User

There are a lot of posts on the web about single file packaging, although it can expand the thinking, quality is general.

Here is the simplest one.

First, add DLLs to your project and change their build action to embedded resources.

Second, referring to the solution given by the CLR Via C #, add a few lines of code, as follows:

1. Add an assembly to load the failed event first.

// To add an Assembly resolution event AppDomain.CurrentDomain.AssemblyResolve + = (sender, args) = LoadFromResource (args. Name);

2. Load the assembly (DLL) according to the file name.

/// <summary>///The corresponding assembly is loaded according to the name of the resource item to be loaded. /// </summary>/// <param name= "Argsname" >The name of the item to resolve. </param>/// <returns>returns the assembly for the corresponding item. </returns>PrivateAssembly LoadFromResource (stringargsname) {    stringDllName =NewAssemblyName (Argsname). Name +". DLL"; varAssem =assembly.getexecutingassembly (); varResourceName = Assem. GetManifestResourceNames (). FirstOrDefault (rn =RN.    EndsWith (DllName)); if(resourcename = =NULL)return NULL;//The assembly was not found.     using(Stream stream =Assem. GetManifestResourceStream (resourcename)) {byte[] Assemblydata =New byte[Stream.        Length]; Stream. Read (Assemblydata,0, assemblydata.length); returnAssembly.Load (Assemblydata); }}

Finish.

C # Single File application

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.