title: How to execute a .NET console-application in memory using C#?
body: <p>From my C# program, I am trying to execute a byte[] containing another .NET executable (a C# console-application) directly from memory and without touching disk.</p>
  
  <p>I am aware that there are similar questions posted here and also some good articles about how to do it like this one:</p>
  
  <p><a href="http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory" rel="nofollow">http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory</a></p>
  
  <p>However, they all run into the same problem, they work BUT only for executing Windows Forms applications in memory, but when I try to execute Console-Applications it doesn't work. Example:</p>
  
  <pre><code>byte[] FileBytes = File.ReadAllBytes("C:\\MyTestProgram.exe");   // just for testing purposes
  
  Assembly a = Assembly.Load(FileBytes);
  MethodInfo m = a.EntryPoint;
  m.Invoke(a.CreateInstance(m.Name), null);
  </code></pre>
  
  <p>And when 'MyTestProgram.exe' is a Windows Form, it executes perfectly. But when 'MyTestProgram.exe' is a Console Application, it returns the following error:</p>
  
  <p>"Unhandled exception: System.Reflection.TargetParameterCountException: Parameter count mismatch"</p>
  
  <p>Please note that the console application doesn't receives any arguments, so this is strange...</p>
  
  <p>Any help would be very much appreciated :)</p>