6

Possible Duplicate:
Load an EXE file and run it from memory using C#

I am using the WebClient class to download a .exe from a web server. Is there a way that I can run the .exe without saving it to disk first?

For the purpose of completeness let me show you what I have so far.

Here is the code I use to start the download:

WebClient webClient = new WebClient();
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
webClient.DownloadDataAsync(new Uri("http://www.somewebsite.com/calc.exe"));

And in the (webClient_DownloadDataCompleted) method I simply grab the bytes from the parameter e:

private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    Byte[] downloadedData = e.Result;
    // how to run this like a .exe?
}

Thank you.

Community
  • 1
  • 1
Jan Tacci
  • 3,131
  • 16
  • 63
  • 83

2 Answers2

2

If your .exe is a .NET program, you can load an assembly and run its entry point.

Otherwise, while there are ways to do it, I can't see the problem with saving a file in temporary directory and running it from there which is so much less painful.

Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
1

Have a look at this thread. I think you can solve it with VirtualAlloc

Is it possible to execute an x86 assembly sequence from within C#?

If your byte array contains a .Net assembly you should be able to do this:

Assembly assembly = AppDomain.Load(byteArray)
Type typeToExecute = assembly.GetType("ClassName");
Object instance = Activator.CreateInstance(typeToExecute);
Community
  • 1
  • 1
Moriya
  • 7,750
  • 3
  • 35
  • 53