Advertisement
ForeverZer0

Embedded Assemblies

Jun 14th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Windows.Forms;
  4.  
  5. namespace EmbeddedAssemblies
  6. {
  7.     static class Program
  8.     {
  9.         /// <summary>
  10.         /// Solution folder containing the embedded assemblies.
  11.         /// </summary>
  12.         private const string ASSEMBLY_FOLDER = "Assemblies";
  13.  
  14.         /// <summary>
  15.         /// The main entry point for the application.
  16.         /// </summary>
  17.         [STAThread]
  18.         static void Main()
  19.         {
  20.             // Set event for when a dependent assembly is referenced and not found.
  21.             AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  22.             Application.EnableVisualStyles();
  23.             Application.SetCompatibleTextRenderingDefault(false);
  24.             Application.Run(new Form1());
  25.         }
  26.  
  27.         private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  28.         {
  29.             var resource = String.Format("{0}.{1}.dll", ASSEMBLY_FOLDER, new AssemblyName(args.Name).Name);
  30.             var assembly = Assembly.GetExecutingAssembly();
  31.             using (var stream = assembly.GetManifestResourceStream(resource))
  32.             {
  33.                 var buffer = new byte[stream.Length];
  34.                 stream.Read(buffer, 0, buffer.Length);
  35.                 return Assembly.Load(buffer);
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement