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

发布于 2024-10-23 14:35:21 字数 150 浏览 1 评论 0原文

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

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

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

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 a signature.

How do I inject the file prior to downloading and reference it later during execution?

Ideally I won't be using a shell to inject the file, rather a native .NET api.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

溺孤伤于心 2024-10-30 14:35:21

您可以使用 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")
});

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

在运行时,您只需使用:

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

检索资源。

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.

七分※倦醒 2024-10-30 14:35:21

要注入文件,请将其添加到您的项目中。然后在解决方案资源管理器中右键单击它,转到属性,并将其类型更改为 EmbeddedResource。

要在运行时加载它,请使用Assembly.GetManifestResourceStream()。在此处阅读更多信息:http://msdn.microsoft.com/en-us/library /xc4235zt.aspx

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.

天荒地未老 2024-10-30 14:35:21

从他所写的内容来看,他似乎会在下载之前动态更改文件。
这实际上取决于您使用的服务器端语言,以及您对服务器/托管提供商帐户的控制程度。
假设您有一个 download.aspx 文件,它会生成此 exe 文件并发送以供下载。
您可以做的一件事是将程序集的源代码放在您的服务器上,然后 download.aspx 对其进行汇编并发送以供下载。 (困难的方法)
另一种方法是将编译后的程序集放在服务器上,然后使用例如 cecil ( 以编程方式嵌入资源在 .NET 程序集中 )或其他任何内容来更改它,然后将其发送以供下载。

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.

行雁书 2024-10-30 14:35:21

这个 Google 结果 似乎很有希望。

This google result seems promising.

青朷 2024-10-30 14:35:21

将文件添加到您的项目中,通常类似于以下内容:

+solution
  +project
    +Resources
      +SomeDirectory
        -SomeFile

然后转到项目的属性,转到左侧站点的资源选项卡,然后选择顶部导航栏上的 files 资源。选择添加资源>添加现有文件。浏览到您刚刚放入项目中的文件并选择添加它。

该文件现在将显示在项目属性的“资源”选项卡下。将“资源”选项卡中的文件名更改为更有意义。

该文件现在是项目的嵌入式资源,您可以通过以下代码访问它:

var MyFile = Properties.Resources.MyFile

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文