Run a program from an array of bytes without creating a temporary file . c#

后端 未结 3 1807
眼角桃花
眼角桃花 2021-01-25 04:47

I have many .exe files stored on IIS server (MSSQL) that contain reports and access to the file(s) on the servers . (These files will be change on Sundays .)

After conne

3条回答
  •  有刺的猬
    2021-01-25 05:18

    Be warned that your belief of any extra security is illusory. If the user has access to the machine to read files, they will also be able to read the memory of your process.

    However, to answer your question, what you are asking to do is simple enough and described here: Load an EXE File and Run It from Memory.

    In essence you do the following:

    1. Pass your byte array to Assembly.Load to create a new Assembly.
    2. Read the entry point of that assembly using the EntryPoint property.
    3. Create an instance using Assembly.CreateInstance, and invoke the method on that instance.

    The code looks like this:

    Assembly a = Assembly.Load(bytes);
    MethodInfo method = a.EntryPoint;
    if (method != null)
        method.Invoke(a.CreateInstance(method.Name), null);
    

提交回复
热议问题