如何在运行时将文件注入EXE并在程序运行期间引用该文件?

goo*_*ate 6 c# deployment user-experience code-injection dll-injection

我希望用户从我的网站下载exe,其中(同步下载时)将一个XML文件注入到该应用程序中.此XML文件包含公钥和签名.

如何在下载之前注入文件并在执行期间稍后引用它?

理想情况下,我不会使用shell来注入文件,而是使用本机.NET API.

Jb *_*ain 8

你可以轻松地使用Mono.Cecil,你只需要写下这样的东西:

var module = ModuleDefinition.ReadModule ("Application.exe");

module.Resources.Add (
    new EmbeddedResource (
        "signature.xml",
        ManifestResourceAttributes.Private, 
        File.ReadAllBytes ("signature.xml")));

module.Write ("Application.exe",
    new WriterParameters {
        StrongNameKeyPair = new StrongNameKeyPair ("keypair.snk")
});
Run Code Online (Sandbox Code Playgroud)

从signature.xml文件注入signature.xml资源,并使用您用于签署Application.exe的keypair.snk重新签名程序集.

在运行时,您只需使用:

var stream = Assembly.GetExecutingAssembly ()
    .GetManifestResourceStream ("signature.xml");
Run Code Online (Sandbox Code Playgroud)

检索资源.