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

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

例如,我的下载代码是:

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是不受管的并且无法合并到项目中。

iCMS 回答:在依赖于内存的EXE C#中下载执行

经过搜索和搜索...。我发现了这个:)

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

本文链接:https://www.f2er.com/2130194.html

大家都在问