如何从内存运行下载的文件? [英] How To Run A Downloaded File From Memory?

查看:51
本文介绍了如何从内存运行下载的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
加载EXE文件并从中运行内存使用C#

我正在使用WebClient类从Web服务器下载.exe.有没有一种方法可以运行.exe而不先将其保存到磁盘?

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"));

在(webClient_DownloadDataCompleted)方法中,我只是从参数e中获取字节:

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?
}

谢谢.

推荐答案

如果您的 .exe 是.NET程序,则可以

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.

这篇关于如何从内存运行下载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆