1

I am developing an application, I want to load exe file (which is an embedded Resource file in my application) and then load Assembly from that byte[] array and run it in main memory. I have followed this Stack Overflow Question and this Article But I am getting this error.

Error: could not load file or assembly

Here is my code:

try
{
    using (FileStream fs = new FileStream(fileName, FileMode.Open))
    {
        BinaryReader br = new BinaryReader(fs);
        byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
        fs.Close();
        br.Close();
        Assembly a = Assembly.Load(bin);
        MethodInfo method = a.EntryPoint;
        if (method != null)
        {
            object o = a.CreateInstance(method.Name);
            method.Invoke(o, null);
        }
        else
        {
            MessageBox.Show("No method found!");
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show("E^RR : " + ex.Message);
}

Note : Embedded resources could be any setup file, in this case I am using file_zilla setup as embedded resource file.

Any help is appreciated.

Community
  • 1
  • 1
Arya Kylee
  • 11
  • 3
  • This is the issue that we don't know about the Setup assembly either it is a .NET assembly or something else, My major target is loading exe (setup) file into main memory. – Arya Kylee Nov 17 '15 at 12:50
  • 1
    Can you write that exe in a temporary directory and then start it using `Process.Start`? – Matteo Umili Nov 17 '15 at 12:54
  • Yes I tried this solution as well and it is working but my requirement is load exe file from resource file and run it directly from main memory. (Setup could be managed or un managed code) – Arya Kylee Nov 17 '15 at 12:57

1 Answers1

1

There could be two issues:

  1. you're trying to load a 64 bit assembly into a 32 bit process or vice versa. That's not possible.
  2. the assembly you're loading is not a .NET assembly. This is the likely case for the FileZilla setup. A .NET assembly is not the exact same as a DLL or EXE. It works for managed code only.

If you are trying to load a regular FileZilla setup, it's not a .NET assembly (screenshot from dotPeek):

FileZilla is not a .NET assembly

If you want to run a native executable, see the second answer of the question you linked.

Community
  • 1
  • 1
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • What about un managed exe file (like in this case we have filezilla.exe file), I want to load exe file directly run from main memory avoiding writing it on secondary storage. – Arya Kylee Nov 17 '15 at 12:53
  • @AryaKylee: see the [second answer](http://stackoverflow.com/a/9233591/4136325) of the linked question. – Thomas Weller Nov 17 '15 at 12:55