2

For example, I download an EXE or MSI file from dataBase as byte array. And I want to run it. I can save it in a file and then Process.Start(path). But if I don't want to create a real file, can I do the same from, for example, Assembly?

Thanks a lot!

starblue
  • 55,348
  • 14
  • 97
  • 151
Artem Makarov
  • 874
  • 3
  • 14
  • 25

2 Answers2

4

If the byte array is a .NET assembly, you can use Assembly.Load with the byte array, then use reflection to work with the types contained within the assembly. This article demonstrates the technique.

This loads it directly into your process, so it works like a referenced assembly.

If the file is a native executable, however, this will not work - another technique will be required. Personally, at this point, I would save it to disk and execute. There are, however, approaches using CreateProcess and inspecting the Process Environment Block. This would likely require unsafe code in C# mixed with P/Invoke to make work correctly, however.

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

Oh, and incidentally, a MSI is not directly executable. It has to be saved to disk and be called via msiexec.

As for the others, consider exactly what you are trying to do. If you have to solve the general case, you will be writing them to disk and executing them too.

Joshua
  • 40,822
  • 8
  • 72
  • 132