问题
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.
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/14202328/how-to-run-a-downloaded-file-from-memory