下载在内存中执行依赖于 EXE C#



我想问一下下载 exe 文件的最佳方法是什么,它依赖于 2 个 dll 文件,以便在不接触磁盘的情况下运行!

例如,我的下载代码是:

private static void checkDlls()
{
string path = Environment.GetEnvironmentVariable("Temp");
string[] dlls = new string[3]
{
"DLL Link 1",
"DLL Link 2",
"Executalbe File Link"
};
foreach (string dll in dlls)
{
if (!File.Exists(path + "\" + dll))
{
try
{
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(dll, path+"\"+dll);
Process.Start(path + "\Build.exe");
}
catch (System.Net.WebException)
{
Console.WriteLine("Not connected to internet!");
Environment.Exit(3);
}
}
}
}

提前感谢您的回答。

PS:我知道缺少内存运行代码,但这就是我要问的,它尚未实现。

我想在内存中运行的文件是一个 C# exe,它需要 2 个 dll 文件才能运行,我想要类似于 https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=netcore-3.1 但用于我的可执行文件的东西。另外,我想知道这将如何影响该过程,因为dll是非托管的,无法合并到项目中。

搜索和搜索后....我找到了这个:)

using System.Reflection;
using System.Threading;
namespace MemoryAppLoader
{
public static class MemoryUtils
{
public static Thread RunFromMemory(byte[] bytes)
{
var thread = new Thread(new ThreadStart(() =>
{
var assembly = Assembly.Load(bytes);
MethodInfo method = assembly.EntryPoint;
if (method != null)
{
method.Invoke(null, null);
}
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return thread;
}
}
}

DLL 您必须将所有 DLL 复制到带有启动器的目录中,以便正在运行的进程可以访问它们。如果您希望将应用程序放在一个文件中,您可以随时将所有应用程序打包在一起并从启动器中解压缩。

也可以准备具有嵌入式库的应用程序。

来源: https://wojciechkulik.pl/csharp/run-an-application-from-memory

最新更新