Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading dll library from Resource to Current Domain(Embedding dll in main exe file)
    text
    copied!<p>I'm trying to load dll libraries during runtime using the following code so that I don't have to provide the user with lot of dll files along with the main executable file. I have inlude all the dll files as an embedded resource and also in the reference part I have include them and have set the <b>CopyLocal</b> property to false. But the problems here are:<br>1. All the dll are getting copied to Bin\Debug folder<br>2. I'm getting <b>FileNotFoundException</b>.<br>I did lot of searches to get these things resolved and finally I'm here. I got a similar code <a href="https://stackoverflow.com/questions/9611154/assemblyresolve-event-fires-when-calling-assembly-loadbyte/13440474#13440474">here</a> but still couldn't do anything. What should I do to prevent this exception...?? Is there a better way to do the same thing for a <b>Windows Form Application(Not WPF)</b>...??</p> <pre><code>using System; using System.Linq; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.Collections.Generic; using System.IO; namespace MyNameSpace { static class Program { static int cnt; static IDictionary&lt;string, Assembly&gt; assemblyDictionary; [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly; if (cnt != 1) { cnt = 1; Assembly executingAssembly = Assembly.GetExecutingAssembly(); string[] resources = executingAssembly.GetManifestResourceNames(); foreach (string resource in resources) { if (resource.EndsWith(".dll")) { using (Stream stream = executingAssembly.GetManifestResourceStream(resource)) { if (stream == null) continue; byte[] assemblyRawBytes = new byte[stream.Length]; stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length); try { assemblyDictionary.Add(resource, Assembly.Load(assemblyRawBytes)); } catch (Exception ex) { MessageBox.Show("Failed to load: " + resource + " Exception: " + ex.Message); } } } } Program.Main(); } if (cnt == 1) { cnt = 2; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.ApplicationExit += new EventHandler(Application_ApplicationExit); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args) { AssemblyName assemblyName = new AssemblyName(args.Name); string path = assemblyName.Name + ".dll"; if (assemblyDictionary.ContainsKey(path)) { return assemblyDictionary[path]; } return null; } } } </code></pre> <p>If I'm using something unnecessarily in my code then you can show me the right way... I'm a student working on <b>Windows Form Application v4.0</b> project for my papers to be submitted.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload