How To Run A Downloaded File From Memory? [duplicate]

天大地大妈咪最大 提交于 2021-02-08 14:00:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!