繁体   English   中英

下载在 Memory 中执行 依赖的 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:我知道 memory 代码中的运行丢失了,但这就是我要问的,它还没有实现。

The file i want to run in memory is a C# exe where it needs 2 dll files in order to run i want something similar to 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;
        }
    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM