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

Download Execute in Memory Depended 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://docs.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;
        }
    }
}

DLLs You have to copy all of your DLLs to the directory with the launcher, so that the running process can access them. In case you would like to have an application in a single file, you can always pack all together and unpack from the launcher.

也可以使用嵌入式库准备应用程序。

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