How can I inject a file into an EXE at runtime and reference the file during program operation?

后端 未结 5 1240
时光取名叫无心
时光取名叫无心 2021-02-06 11:22

I\'d like a user to download an exe from my website, where (synchronously upon download) an XML file is injected into this application. This XML file contains a public key, and

相关标签:
5条回答
  • 2021-02-06 11:31

    You could that easily with Mono.Cecil, you'd just have to write something like:

    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")
    });
    

    To inject the signature.xml resource from the signature.xml file, and sign back your assembly with your keypair.snk that you used to sign Application.exe.

    And at runtime, you'd just have to use:

    var stream = Assembly.GetExecutingAssembly ()
        .GetManifestResourceStream ("signature.xml");
    

    To retrieve the resource.

    0 讨论(0)
  • 2021-02-06 11:33

    From what he writes it seems he's gonna dynamically change the file prior to download. This really depends on the server-side language you use, And how much control you have over your server/hosting provider account.
    Say you have a download.aspx file which generates this exe files and sends for download. One thing you can do is to put the assembly's source on your server then download.aspx assembles it and send it for download. (The difficult way)
    Another way is to put the compiled assembly on server then use e.g cecil ( Programmically embed resources in a .NET assembly ) or whatever to change it then send it for download.

    0 讨论(0)
  • 2021-02-06 11:39

    Add the file to your project, typically something along the lines of:

    +solution
      +project
        +Resources
          +SomeDirectory
            -SomeFile
    

    Then go to your project's properties, go to the resources tab on the left site and select the files resource on the top nav bar. Select to add a resource > add existing file. Browse to the file you just put into your project and select to add it.

    The file will now show up under your Resources tab of your project's properties. Change the name of your file in the Resources tab to be more meaningful.

    The file is now an embedded resource of your project and you can access it by the following in code:

    var MyFile = Properties.Resources.MyFile
    
    0 讨论(0)
  • 2021-02-06 11:45

    This google result seems promising.

    0 讨论(0)
  • 2021-02-06 11:57

    To inject the file add it to your project. Then right-click on it in the solution explorer, go to properties, and change its type to EmbeddedResource.

    To load it at run-time use Assembly.GetManifestResourceStream(). Read more here: http://msdn.microsoft.com/en-us/library/xc4235zt.aspx.

    0 讨论(0)
提交回复
热议问题